dll-import in c# with (const char far *...)



  • Hi All,

    I'm rather new to C# and extremely new to Interop.
    My problem: I have a C DLL that I need to call and I'm trying to do it in
    C#. I can call the function but for some reason my C DLL doesn't seem to be
    getting what I'm trying to pass it.

    C function: BYTE __declspec(dllexport) StationOnline(const char far * );

    my C# call is: [DllImport("xxxxxxx.dll", EntryPoint = "StationOnline")]
    public static extern byte StationOnline(string
    sStaName);

    Anyways, my question is if I am declaring everything correctly. Even when I
    know my StationOnline function should be 1 I'm getting 0.

    Thanks in advance,
    Greg



  • Greg1970 schrieb:

    Hi All,

    I'm rather new to C# and extremely new to Interop.
    My problem: I have a C DLL that I need to call and I'm trying to do it in
    C#. I can call the function but for some reason my C DLL doesn't seem to be
    getting what I'm trying to pass it.

    C function: BYTE __declspec(dllexport) StationOnline(const char far * );

    my C# call is: [DllImport("xxxxxxx.dll", EntryPoint = "StationOnline")]
    public static extern byte StationOnline(string
    sStaName);

    Anyways, my question is if I am declaring everything correctly. Even when I
    know my StationOnline function should be 1 I'm getting 0.

    Thanks in advance,
    Greg

    data types

    unmanaged data types | managed data types(C#)
    ----------------------------------------------------------------
    BYTE | System.Byte

    **const char *** | System.String or System.StringBuilder

    [DllImport("xxxxxxx.dll", EntryPoint = "StationOnline")]
    public static extern System.Byte StationOnline(System.String
    sStaName)
    

    look at http://msdn2.microsoft.com/de-de/ac7ay120.aspx 👍

    or

    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconplatforminvokedatatypes.asp



  • Well isn't System.String mapped to string and System.Byte mapped to byte in C#? AFAIK there are no language-internal datatypes that don't map to a Framework Type.

    According to http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpcondefaultmarshalingforstrings.asp a c-string must be marshaled when converted to a framework string:

    [DllImport("xxxxxxx.dll", EntryPoint = "StationOnline")]
    public static extern byte StationOnline([MarshalAs(UnmanagedType.LPStr)] string
    sStaName);
    

Anmelden zum Antworten