Click to See Complete Forum and Search --> : Passing/Returning Strings to a C++ DLL


wrxhokie
March 9th, 2009, 03:29 PM
I'm having trouble passing strings to a C++ DLL from my vb.net program. I get constant Read/Write memory error exceptions. My C++ code is as follows:

char * get_signal_desc(char * dev)
{
bool status;
char * desc;
status = get_dev_signal_desc(dev, desc);
return desc;
}

And i'm using this function in my vb.net code as follows:

Public Declare Function get_signal_desc Lib "Core_Wrapper.dll" (ByVal sig As String) As String

Dim sig As String
Dim desc As string
desc.= Space(255)
sig = "MRG31025"
desc = get_signal_desc(sig)
MessageBox.Show(desc.ToString)


I get the exception at the line where i call the C++ function. Can anyone help me figure out how to pass strings between a C++ DLL and vb.net program?

wrxhokie
March 10th, 2009, 09:24 AM
Its solved. I found you have to use <MarshalAs(UnmanagedType.LPStr)> for all string types. And if your DLL returns a char *, then use stringbuilders.