Passing/Returning Strings to a C++ DLL
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:
Code:
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:
Code:
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?
Re: Passing/Returning Strings to a C++ DLL
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.