none
Passing parameters to a C++ DLL from C#

    Question

  • Hi,

    I'm trying to call a C++ function from C# (using VS 2005). When I debug I see that the execution flow reaches the C++ function, but the parameters are wrong: the first parameter (host) has the value that I passed to the third one (serverId). What am I doing wrong?

    This is the C++ function:
    extern "C" __declspec(dllexport) bool initialize_event_source(
    	const string host, const string port, const string serverId, string& outMsg);

    And this is how I'm trying to import it from C#:

            [DllImport("Comm3D2D.dll", CallingConvention=CallingConvention.Cdecl)]
            [return : MarshalAs(UnmanagedType.Bool)]
            extern static public bool initialize_event_source(
                [MarshalAs(UnmanagedType.LPStr)] string host,
                [MarshalAs(UnmanagedType.LPStr)] string port,
                [MarshalAs(UnmanagedType.LPStr)] string serverId,
                [MarshalAs(UnmanagedType.LPStr), Out] StringBuilder outMsg);
    

    Thanks in advance!

    Miguel
    Wednesday, May 20, 2009 3:31 PM

Answers

  • Hi,

    I think this really depends how you pass the the parameters and what parameter value you passed in.

    Use the debug to see the parameters' value before and after you passing in, compare it. You also need to check the c++ function to see whether the inside logic is correct.

    Thanks
    Binze
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Friday, May 22, 2009 10:51 AM

All replies

  • Hi again, I'm sorry for last replay.
    Anyway, I hope this replay and information might help you.

    Your C++ function seems correct:
    extern "C" __declspec(dllexport) bool initialize_event_source(
    const string host, const string port, const string serverId, string& outMsg);

    This is ONLY the import of dll and a static bool function:
    [DllImport("Comm3D2D.dll", CallingConvention=CallingConvention.Cdecl)]
            [return : MarshalAs(UnmanagedType.Bool)]
            extern static public bool initialize_event_source(
                [MarshalAs(UnmanagedType.LPStr)] string host,
                [MarshalAs(UnmanagedType.LPStr)] string port,
                [MarshalAs(UnmanagedType.LPStr)] string serverId,
                [MarshalAs(UnmanagedType.LPStr), Out] StringBuilder outMsg);

    Can you provide more data, like how do you pass the parameters? and What is the "print" result etc?

    Have a nice day...

    Sincerely,
    Fisnik


    Coder24.com
    Wednesday, May 20, 2009 3:45 PM
  • Thaks for your answer.

    I'm calling the function from C# like this:

    StringBuilder msg = new StringBuilder("teststring");
    if (Comm3D2D.initialize_event_source("localhost", "12345", "ATC_Event_Source", msg))


    And checking with the debugger inside the C++ DLL I get the following values for the parameters:

    host: "ATC_Event_Source" (should be "localhost")
    port: "" (should be "12345")
    serverId: some binary stuff (should be "ATC_Event_Source")
    outMsg: "" (should be "teststring", but it's an output parameter anyway)

    Wednesday, May 20, 2009 4:22 PM
  • Hi again:

    I'm wondering if there may be a problem, because you are using a "bool", which means true/false.
    But when you throw the function, you seem not verifing the "bool" (I mean wether it's true/false). 

    Have a nice day...

    Sincerely,
    Fisnik  


    Coder24.com
    Wednesday, May 20, 2009 4:27 PM
  • Hi again:

    You should test this:
    [DllImport("Comm3D2D.Dll")]
    
    public static extern void initialize_event_source([MarshalAs(UnmanagedType.LPWStr)String host); //And the rest.
    
    

    I hope this helped you...

    Have a nice day...

    Sincerely,
    Fisnik


    Coder24.com
    Wednesday, May 20, 2009 4:33 PM
  • I'll do that and try to add the parameters one by one to see what happens.

    Thanks a lot!

    Miguel
    Wednesday, May 20, 2009 4:40 PM
  • Hi again:

    Good! Tell me if it worked, if it doesn't work, just contact me here...I'm waiting...

    Have a nice day...

    Sincerely,
    Fisnik
    Coder24.com
    Wednesday, May 20, 2009 4:42 PM
  • Hi,

    I think this really depends how you pass the the parameters and what parameter value you passed in.

    Use the debug to see the parameters' value before and after you passing in, compare it. You also need to check the c++ function to see whether the inside logic is correct.

    Thanks
    Binze
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Friday, May 22, 2009 10:51 AM
  • One little step forward: the problem seems to be the std::string type. If I replace it with char* I get the input parameters and the return value to work (the output parameter is not yet working). I believe that either I'm using the wrong marshaling type (LPStr) or the std::string type, being a C++ type, doesn't play well with the C calling conventions. Any suggestions are welcome.

    Thanks again,
    Miguel
    Monday, May 25, 2009 8:41 AM