将字符串从C ++ dll返回到vb

时间:2013-11-02 05:35:12

标签: c++ vb.net dll

我有以下dll cpp文件

#include "stdafx.h"
#include <string.h>
#include <Windows.h>
#include <OleAuto.h>
using namespace std;
extern "C" __declspec(dllexport) BSTR func(BSTR a) 
{
BSTR buffer;
buffer = SysAllocString(a);
 return buffer;
}

在vb结束     

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim string1 As String
    string1 = func(TextBox1.Text)
    TextBox2.Text = string1
End Sub

上面的代码编译正确但表单在文本框中显示字符串之前关闭。但是,如果我将文本作为整数返回,它可以正常工作......

1 个答案:

答案 0 :(得分:1)

BSTR!= VB.NET字符串我认为,,,使用LPCSTR

extern "C" __declspec(dllexport) LPCSTR func(LPCSTR a)