如何将字符串从csharp程序传递到vc++dll



我们想要将一个字符串从csharp程序传递给vc++。

以下是代码:在C#中

    [DllImport("ConsoleApplication2.dll")]
    public static extern int main_c(StringBuilder IpAddr, int p);
    public string[] tcp()
    {            
        StringBuilder buffer = new StringBuilder("192.168.1.100");                       
        int i = main_c(buffer, 34318);

在vc++中

extern __declspec( dllexport ) int main_c(char *peer,int port)

这给出了一个错误,即":main_c'已使堆栈不平衡。"如何做到这一点?

就个人而言,我会尝试这样声明:

[DllImport("ConsoleApplication2.dll", CallingConvention=CallingConvention.Cdecl)]
public static extern int main_c([MarshalAs(UnmanagedType.LPStr)] String IpAddr, int port);

并在VC++函数中声明指针常量,因为它不应该在那里写入。您甚至不需要StringBuilder。

最新更新