on 09-10-2014 1:23 PM
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..
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
85 | |
10 | |
10 | |
10 | |
7 | |
6 | |
6 | |
5 | |
4 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.