Click here to Skip to main content
15,844,477 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi, every body

i have a little problem in p/invoke :
my function signature in c++ is some thing like this

C++
ReadData (char* data);


that i will pass address of string to that fucntion and my data will be filled in the address (function works fine but just data marshaling goes wrong and cause in error), now i want to used it in C# witch type of .net data type must be used
to properly working

i used this in C# but error happend.
C#
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
public delegate int ReadData (out String data);

IntPtr readAddrs = NativeSupport.GetProcAddress(dll, "ReadData");
            ReadData read = (MS_Read)Marshal.GetDelegateForFunctionPointer(readAddrs, typeof(ReadData));

string d = "";
read(out d);
//Attempted to read or write protected memory. This is often an indication that //other memory is corrupt. 



what kind of marshaling must be used.
tanks in advance .
Posted

1 solution

Don't use a string - they are immutable (i.e. they cannot be changed). Use a StringBuilder instead and set it's initial size to the maximum your routine expects to return, and I suspect that it should be a ref parameter rather than an out. Without seeing the code, I can't tell though. It may be that just handing it through as a parameter without out or ref will work fine, if ReadData does not modify it.
 
Share this answer
 
Comments
Areff 22-Feb-12 3:34am    
ReadData modifies it , and actually ReadData is responsible for reading data from a device and i havenot access to it's code , i just use it and it is working as well as expected in c++ app , it works in P/Invoke but in end of execution and in it's marshaling state error happend.
i changed the code as this code :

[UnmanagedFunctionPointer(CallingConvention.StdCall)]
public delegate int ReadData (ref StringBuilder data);

IntPtr readAddrs = NativeSupport.GetProcAddress(dll, "ReadData");
ReadData read = (MS_Read)Marshal.GetDelegateForFunctionPointer(readAddrs, typeof(ReadData));


StringBuilder d = new StringBuilder(200);
read(ref d);
/"Attempted to read or write protected memory. This is often an //indication that other memory is corrupt."

but error occurred again. using both ref and out ends with same result (the error).
OriginalGriff 22-Feb-12 4:15am    
Does ReadData modify the pointer, or just fill the data area?
Areff 22-Feb-12 5:12am    
i think it will fill the data area (i have not access to source code of ReadData function)
OriginalGriff 22-Feb-12 5:21am    
Then you don't need (or want) out or ref - just declare it as
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
public delegate void ReadData (StringBuilder data);
and remember to allocate enough memory to the StringBuilder as ReadData requires (plus a bit, to be on the safe side).

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900