using pinvoke to fill a string

G

Guest

I have a 3rd party unmanged .dll. This has functions that will look up
information and fill a C string passed to it with data. I want to use this
data in .NET classes after it has been filled and I am programming in C++. I
believe that I should use pinvoke, but I see a problem that I can't figure
how to get around. I don't think I can declare the string I want as a String
class since when I pass it to unmanged function it is not yet allocated and
the unmanged function does not allocate space it just fills it. So I would
think this would cause memory corruption. I was also thinking that I could
create the String class and fill it with blanks so memory was allocated, but
I don't know if the marshling will correctly change value since String class
are basically a static value unless you use correct member function that
deletes old value and then adds new value.

If any one can tell me the correct way to set this up it would be very much
appreciated.
Thanks
 
G

Guest

Brian.. In C++/cli given"

bool Copy(char* out,int size,const char * in) {
strcpy_s(out,size,in);
bool isNullTerminated= false;
if (in[size-1] == '\0')
isNullTerminated= true;
return isNullTerminated;
}

You can do:

// exercise String to Ansi Ansi to String
String^ Echo(String^ in) {
NativeChild nc;
IntPtr ip(0);
String^ out=L"";
char* str= 0;
try {
str= new char[in->Length+1]; // null terminator
ip = Marshal::StringToHGlobalAnsi(in); // create char[] on unmanaged heap
nc.Copy(str,in->Length+1,static_cast<const char*>(ip.ToPointer()));
out= Marshal::ptrToStringAnsi(static_cast<IntPtr>(str));
}
catch(...) {
out= L"exception thrown";
}
finally {
if (ip != IntPtr(0)) {
Marshal::FreeHGlobal(ip); // release char[] on unmanaged heap
}
if(str) {
delete [] str; // release char[] on unmanaged heap
}
}
return out;
}

:)
 
G

Guest

Thanks, that got me a bit further. If you don't mind I do have a couple of
questions about your syntax.
You use the declaration of a string as String^ not String * which is what I
had to use to get it to work.
Also in your intilization of out you use L"" , I have seen programs using
S"some string", but not L. I have not been able to find any documentation on
what these stand for or when you need to use them. If you could tell me
where to find information on this it would be appreciated. I presume that
there are also other prefixes that have some other meanings.

JAL said:
Brian.. In C++/cli given"

bool Copy(char* out,int size,const char * in) {
strcpy_s(out,size,in);
bool isNullTerminated= false;
if (in[size-1] == '\0')
isNullTerminated= true;
return isNullTerminated;
}

You can do:

// exercise String to Ansi Ansi to String
String^ Echo(String^ in) {
NativeChild nc;
IntPtr ip(0);
String^ out=L"";
char* str= 0;
try {
str= new char[in->Length+1]; // null terminator
ip = Marshal::StringToHGlobalAnsi(in); // create char[] on unmanaged heap
nc.Copy(str,in->Length+1,static_cast<const char*>(ip.ToPointer()));
out= Marshal::ptrToStringAnsi(static_cast<IntPtr>(str));
}
catch(...) {
out= L"exception thrown";
}
finally {
if (ip != IntPtr(0)) {
Marshal::FreeHGlobal(ip); // release char[] on unmanaged heap
}
if(str) {
delete [] str; // release char[] on unmanaged heap
}
}
return out;
}

:)

brian_harris said:
I have a 3rd party unmanged .dll. This has functions that will look up
information and fill a C string passed to it with data. I want to use this
data in .NET classes after it has been filled and I am programming in C++. I
believe that I should use pinvoke, but I see a problem that I can't figure
how to get around. I don't think I can declare the string I want as a String
class since when I pass it to unmanged function it is not yet allocated and
the unmanged function does not allocate space it just fills it. So I would
think this would cause memory corruption. I was also thinking that I could
create the String class and fill it with blanks so memory was allocated, but
I don't know if the marshling will correctly change value since String class
are basically a static value unless you use correct member function that
deletes old value and then adds new value.

If any one can tell me the correct way to set this up it would be very much
appreciated.
Thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top