Problem with api call
hi
im busy porting an old c++ project to c# (console) and having some issues with an API call
the c++ api call is
char p_DllVersion( char* Version);
where the useage would be
char version[256];
p_DllVersion(&version);
printf("Version : %s",version);
but im not getting this right in c# i tried
[DllImport("c:\\p.dll")]
public static extern char p_DllVersion(ref String Version);
with useage
String version;
p_DllVersion(ref version);
printf("Version : %s",version);
but im getting error message
Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
also tried to use out instead of ref and tried
public static extern char p_DllVersion([Out] String Version);
with no sucess ;(
please help
2 Answers
Know the answer? Post it. Somebody with the same question will find it here.

AlanRank #669 · 3.6K reputationPosted Jul 28, 2007, 2:31 AM EDT
See if this will work:
using System.Text;
....
[DllImport("c:\\p.dll")]
public static extern char p_DllVersion(StringBuilder sbVersion);
and call with:
StringBuilder sbVersion = new StringBuilder(256);
p_DllVersion(sbVersion);
string version = sbVersion.ToString();
printf("Version : %s",version);
Daniel van Pletzen25 reputationPosted Jul 29, 2007, 1:04 AM EDT