The StringToHGlobalAnsi method might be returning a Ptr to an ASCIIZ string rather than a "STRING" string. (OLE string).
In which case you just have to change the procedure header in your PB function to either "[BYREF] sz AS ASCIIZ" or "BYVAL psz AS ASCIIZ PTR"
Worth a try anyway.
Announcement
Collapse
No announcement yet.
.net c# webservice to PB dll
Collapse
X
-
.net c# webservice to PB dll
Hi
I hope someone can help me solve this riddle with c#.
I'm trying to write a webservice that calls a PB dll (v8.04).
My dll (besides a clean LibMain) is this:
Code:FUNCTION Test1 ALIAS "TEST1" ( ProjectID AS LONG, sVar AS STRING ) EXPORT AS LONG FUNCTION = LEN( sVar ) END FUNCTION FUNCTION Test2 ALIAS "TEST2" ( ProjectID AS LONG, sVar AS STRING * 255 ) EXPORT AS LONG FUNCTION = LEN(TRIM$( sVar )) END FUNCTION
In my c# webservice, I have the following declarations and webmethod:
Code:[DllImport("test.dll", EntryPoint = "TEST1", SetLastError = true, CharSet = System.Runtime.InteropServices.CharSet.Auto)] private static extern int test1( int projectid, [MarshalAs(UnmanagedType.AnsiBStr,SizeConst=255)] string sVar ); [DllImport("test.dll", EntryPoint = "TEST2", SetLastError = true, CharSet = System.Runtime.InteropServices.CharSet.Auto)] private static extern int test2( int projectid, [MarshalAs(UnmanagedType.AnsiBStr,SizeConst=255)] string sVar ); [WebMethod] public int RunTest1(string sVar) { try { return test1(1, sVar); } catch (Exception ex) { return -1; } } [WebMethod] public int RunTest2(string sVar) { try { return test2(1, sVar); } catch (Exception ex) { return -1; } }
I've tried playing with leaving the SizeConst in the webdeclaration and AnsiBStr and I just cannot figure out what I'm doing wrong here.
What I want to archieve in the end is how to pass a pointer (of a string or a struct):
Additional function in PB
Code:FUNCTION Test3 ALIAS "TEST3" ( ProjectID AS LONG, BYVAL sVar AS STRING PTR ) EXPORT AS LONG FUNCTION = LEN(TRIM$( @sVar )) END FUNCTION
Code:[DllImport("test.dll", EntryPoint = "TEST3", SetLastError = true, CharSet = System.Runtime.InteropServices.CharSet.Auto)] private static extern int test3( IntPtr sVar ); public int RunTest3(string sQuestion) { try { IntPtr s = Marshal.StringToHGlobalAnsi(sQuestion); int result = test3(1, ref s); Marshal.FreeHGlobal(s); return result; } catch (Exception ex) { return -1; } }
Hope someone can give me tip on where to go with this.
Many regards
Jeroen BrouwersTags: None
-
Leave a comment: