cancel
Showing results for 
Search instead for 
Did you mean: 

How to call VC++ dll whiich return char[] data type value in powerbuilder

Former Member
0 Kudos

Hi Everyone,

I am using PowerBuilder 11.1 build 8123.

I am calling VC++ DLL in our PowerBuilder application which return char[] data type value.

I just declare Global External Function:-

     Function  char getOSSectionName() library "DocServClient.dll" alias for "getOSSectionName;Ansi"

And then call this function in window as:-

     Char      ls_section[1] = 'DOCAPPLICATIONS9X'

     ls_section[1]=getOSSectionName()

When I check ls_section[1] value in messagebox it display a symbol (please check attached attached image.)

VC++ function and return variable declaration:-

     __declspec(dllexport) char *  __stdcall getOSSectionName()

      {

       char    SectionName[_MAX_PATH];

Please let me know that my code is valid or not.

Please help..

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Hi,

Thanks to Everyone for your suggestions.

I am solved my problem to change VC++ function as :-

     Int __declspec(dllexport)  __stdcall getOSSectionName(char* SectionName)

                    instead of

     __declspec(dllexport) char *  __stdcall getOSSectionName()

and declare global function as:-

       Function int getOSSectionName(Ref char ls_section[100]) library "DocServClient.dll" alias for "getOSSectionName;Ansi"

and call function in window as:-

     int li_ret

     char ls_section[100]

     string ls_sec

     li_ret=getOSSectionName(REF ls_section)

     ls_sec=ls_section

Thanks,

Vikrant

Former Member
0 Kudos

Once again - a poorly designed and written piece of code that is built on assumptions.  Overflow that string in your DLL function (because the function does not know the size of the buffer provided) and what happens?  BOOM!   MAX_PATH is much larger than 100 character - and any logic built on that particular #DEFINE should be using MAXPATH + 1 as the size of the allocated buffer.

There is no reason to convert between Unicode and ansi as far as I can tell.

Neither is there a reason to use a char array and do the char array/string copy.

Answers (3)

Answers (3)

former_member190719
Active Contributor
0 Kudos

The PowerBuilder equivalent of a char * is string (if you are not including null characters in the return) or a blob (if you are including null characters).  Try replacing char with string in your RPCFUNC declaration.  If it was an argument being passed by reference you would need to pad it with spaces (or other data) before sending it.  Since it's a return value you don't need to do that.

http://www.sybase.com/detail?id=44648

Former Member
0 Kudos

Start over.  Have you seen the API functions that return blobs of information (strings, buffers, etc.)?  For example, GetComputerName: http://msdn.microsoft.com/en-us/library/windows/desktop/ms724295(v=vs.85).aspx

There is a reason they are structured in this manner.  Your function should not presume to allocate memory and then "give" it to the process which called the function.  Rather, the calling process (your PB app) will allocated space for the return value and the length of the memory provided - your function will then stuff the appropriate value into that buffer and return the length of the value provided.  You will need to determine how to handle (in your DLL function) situations where the caller did not provide sufficient space.

With regard to your current code - As Chris implies, your function returns a string (really, pointer to char).  In PB you declared that the function returned a single char.  PB is expecting a char, not a pointer - and it is interpreting part of the returned pointer as a char.  It is doing exactly what you told it to do - unfortunately.

Former Member
0 Kudos

Hi Vikrant;

1) Both VS and PB is now Unicode - thus, you need to remove the ";ansi" designation in the external declaration.

2) The external declaration should be an return of an array ...

Function char[] getOSSectionName() library "DocServClient.dll"

3) You need to declare your PB work variable as an unbounded array ...

Char  ls_section[ ] = {'DOCAPPLICATIONS9X'}

HTH

Regards ... Chris

Former Member
0 Kudos

Hi Chris,

Thanks for your suggestion, but when i declare external function as return as Char array, PowerBuilder show syntax error.

Thanks,

Vikrant

Former Member
0 Kudos

Hi Vikrant;

  Oh Dear ... this works in PB.Net but not in PB Classic!  

Looks like you need to open a support case with SAP on this one if you want to use the Classic version of PB for this.

Regards ... Chris

Former Member
0 Kudos

Hello Chris:

Thanks for your response sir.

I am new to VC++.  Where do I mark my method as "_stdcall"?

I appriciate your help Chris!

Kind Regards,

David