Quantcast
Channel: Common Language Runtime Internals and Architecture forum
Viewing all articles
Browse latest Browse all 1710

C# Marshalling StringBuilder to char*, always NULL upon entry to function

$
0
0

Hi,

I’m attempting to use the a C library from C# code. The method that I would like to use has the following prototype:

 long Convert_Geodetic_To_MGRS (double Latitude,
                                                           double Longitude,
                                                           long Precision,
                                                           char* MGRS);

My C# code for importing the library is as follows:
[DllImport("geotrans2.dll", EntryPoint="#168", CallingConvention=CallingConvention.Cdecl)]
private static extern long Convert_Geodetic_To_MGRS (double Latitude,double Longitude,long Precision,StringBuilder MGRS);

And here is my C# code where I call the method:
double Latitude = 1;
double Longitude = 2;
 int Precision = 5;
StringBuilder str = new StringBuilder(256);
long ret = Convert_Geodetic_To_MGRS (Latitude, Longitude, Precision,str);

The Latitude, Longitude, and Precision parameters are coming into the function just fine, but I am having problems with the char* MGRS parameter…no matter what I pass in, the MGRS parameter is NULL upon entry to the function. I know this because I have added ASSERT(MGRS != NULL) into the DLL source and recompiled the DLL, and this asserts every time I make the method call.

 Besides StringBuilder for the type of the MGRS parameter I have also tried:
1) ref StringBuilder
2) out StringBuilder
3) string
4) ref string
5) out string
6) char[]
7) ref char[]
8) out char[]
9) byte[]
10) ref byte[]
11) out byte[]

Nothing seems to work, the MGRS parameter is always NULL upon entry to the DLL function.

According to the .NET help files, StringBuilder should be marshaled as a [in,out] char*...no clue why it is NULL upon entry to the DLL function.

Any help would be greatly appreciated.

Thanks,
Russ


Viewing all articles
Browse latest Browse all 1710

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>