Posted on 2004-08-05
I been having issues with a 3rd party unmanaged DLL which I use to access a database via an api. I am trying to pass in a string value which the documentation states as a pointer (char*). I have next to no C++ experience so it could be a real obvious one !
the C++ api declaration reads as follows
// Assume TM1V is an int
TM1IMPORT TM1V TM1API TM1SystemServerHandle( TM1U hUser, CHAR * szName );
and is called in the demo client C++ app like so
TM1SystemServerHandle( hUser, sServerName );
I found that using a C# string value for the second paramter was causing issues so I changed the API call to Marshal the value as LPSTR as follows
[DllImport("tm1api.dll")] private static extern int TM1SystemServerHandle (int hUser ,[MarshalAs(UnmanagedType.LPStr)] string ipntName );
and called like follows
//assume huser is a valid handle
StringBuilder sb=new StringBuilder(myString,255);
int ihTest = TM1SystemServerHandle(hUser, sb.ToString());
this produces the same issues (occasional null return) as when I passed the string in as of type string. I have also changing the api to accept a Stringbuilder and also IntPnt generated from Marshal.StringToHGlobalAnsi.
I sed to have similar issues when returning strings from the api but after some help I found that by returning an IntPnt and using Marshal.PtrToStringAnsi and it all worked fine.
Not sure what else to try. Is there any other was to handle pointers ?
thanks in advances