Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Calling C++ function using LPWSTR from C#

giridhar_madgiridhar_mad Member Posts: 3
Hi!!!

I've a C++ library (.h and .lib), which I want to use in my C# application. For which, I've written a managed C++ wrapper over this lib, that can be used in the C# app.
One of the functions has a parameter of type LPWSTR.
Eg : SomeFunc(LPWSTR param1)

LPWSTR is a macro defined for WCHAR* and WCHAR is a macro for wchar_t.

How do I call this SomeFunc() from my C# application? What type should I use for the parameter?

Any code snippet will be of great help!!!

Thanks!!!
Vihi

Comments

  • tsagldtsagld Member Posts: 621
    : Hi!!!
    :
    : I've a C++ library (.h and .lib), which I want to use in my C# application. For which, I've written a managed C++ wrapper over this lib, that can be used in the C# app.
    : One of the functions has a parameter of type LPWSTR.
    : Eg : SomeFunc(LPWSTR param1)
    :
    : LPWSTR is a macro defined for WCHAR* and WCHAR is a macro for wchar_t.
    :
    : How do I call this SomeFunc() from my C# application? What type should I use for the parameter?
    :
    : Any code snippet will be of great help!!!
    :
    : Thanks!!!
    : Vihi
    :

    Have you tried [b]string[/b] or [b]ref string[/b]?

    Greets,
    Eric Goldstein
    www.gvh-maatwerk.nl

  • iwilld0itiwilld0it Member Posts: 1,134
    Here is an example ...

    [code]
    [DllImport("StringLib.Dll")]
    public static extern void PassLPWStr([MarshalAs(UnmanagedType.LPWStr)String s);
    [/code]

    The MarshalAs attribute is in the System.Runtime.InteropServices namespace.

    Cheers.

  • giridhar_madgiridhar_mad Member Posts: 3
    : : Hi!!!
    : :
    : : I've a C++ library (.h and .lib), which I want to use in my C# application. For which, I've written a managed C++ wrapper over this lib, that can be used in the C# app.
    : : One of the functions has a parameter of type LPWSTR.
    : : Eg : SomeFunc(LPWSTR param1)
    : :
    : : LPWSTR is a macro defined for WCHAR* and WCHAR is a macro for wchar_t.
    : :
    : : How do I call this SomeFunc() from my C# application? What type should I use for the parameter?
    : :
    : : Any code snippet will be of great help!!!
    : :
    : : Thanks!!!
    : : Vihi
    : :
    :
    : Have you tried [b]string[/b] or [b]ref string[/b]?
    :
    : Greets,
    : Eric Goldstein
    : www.gvh-maatwerk.nl
    :
    :
    I tried using it. It gives the same compile error "Cannot convert from string to char*"

  • giridhar_madgiridhar_mad Member Posts: 3
    : Here is an example ...
    :
    : [code]
    : [DllImport("StringLib.Dll")]
    : public static extern void PassLPWStr([MarshalAs(UnmanagedType.LPWStr)String s);
    : [/code]
    :
    : The MarshalAs attribute is in the System.Runtime.InteropServices namespace.
    :
    : Cheers.
    :
    :
    I tried using the Marshalling. It does not make any difference. It gives the same compile error "Cannot convert from string to char*"
  • iwilld0itiwilld0it Member Posts: 1,134
    : : Here is an example ...
    : :
    : : [code]
    : : [DllImport("StringLib.Dll")]
    : : public static extern void PassLPWStr([MarshalAs(UnmanagedType.LPWStr)String s);
    : : [/code]
    : :
    : : The MarshalAs attribute is in the System.Runtime.InteropServices namespace.
    : :
    : : Cheers.
    : :
    : :
    : I tried using the Marshalling. It does not make any difference. It gives the same compile error "Cannot convert from string to char*"
    :

    Well then LPWStr is not going to work because LPWSTR is not char* ... tell me this, does your C++ library stuff data into the string that you are passing as an argument ... if so then you can use a string builder ...



    [code]
    using System.Text;
    // ...

    [DllImport("StringLib.Dll")]
    public static extern void PassStr(StringBuilder s);
    [/code]

    To use ...

    [code]
    StringBuilder data = new StringBuilder(255);
    PassStr(data);
    [/code]

    Have to initialize the stringbuilder capacity (255 for ex.)



Sign In or Register to comment.