CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Jun 2008
    Posts
    24

    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?

  2. #2
    Join Date
    Jun 2008
    Posts
    24

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured