开发者

Pass string to dll

How to call a function in c++ dll from C# WPF application with string parametrs? I add dll to C# app through "Add References", not "[DLLImport]"

All problem is that C# suggests function 开发者_开发问答with 'sbyte*' parametr, not 'char*'. Functions with 'int' parametrs works perfect.

Here is code:

My function in c++ dll:

public ref class OpenGLHwnd : public HwndHost   
{
public:
    void myFunc(char* name)
    {
        // some code
    }

C# code:

HwndHost host = new WPFOpenGLLib.OpenGLHwnd();
HWNDPlaceholder.Child = host;
(HWNDPlaceholder.Child as WPFOpenGLLib.OpenGLHwnd).myFunc("aaaa");

myFunc want 'sbyte*' parametr, not 'char*'. Why?

Or tell, please, how to convert string to sbyte*


Since your C++ DLL is apparently a .NET assembly (ref class, i.e. C++/CLI), you can just pass the string as .NET String:

#include <msclr/marshal.h>
using namespace msclr::interop;

public ref class OpenGLHwnd : public HwndHost   
{
public:
    void myFunc(System::String^ name)
    {
        marshal_context^ context = gcnew marshal_context();
        const char* ptr = context->marshal_as<const char*>(name);
        puts(ptr);
        delete context;
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜