 |
只要你用new char(20)先分配个地址空间给Str就可以了(而用说明带赋值的办法是不行的)
刘映虹的意见:
VB的字符串格式是BSTR,如果你了解COM编程,你对这个类型不会陌生,而DLL中的字符串变量是标准C字符串格式,你应该将返回的字符串变量的类型定义为类_bstr_t,它是VC的类,封装了BSTR的操作,在MFC和ATL中都可用。
panda的意见:
VB中的字符串是用SysAllocString申请空间的,如果不想用COM的话把VC中的函数改为:
EXTERN_C BSTR WINAPI RetStr()
{
char *str="1234567890";
return SysAllocString((BSTR)str);
}
返回给VB后,VB会自动释放空间。
Tianjie Mao的意见:
下面的代码应该可以(无论是中文还是英文都能正确处理)
VC部分:
//DllMain.h文件:
#if !defined(AFX_STDAFX_H__F34DAFE6_26B9_45B2_A16C_88318F51D7C7__INCLUDED_)
#define AFX_STDAFX_H__F34DAFE6_26B9_45B2_A16C_88318F51D7C7__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
// Insert your headers here
#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
#include <windows.h>
// TODO: reference additional headers your program requires here
//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.
#endif // !defined(AFX_STDAFX_H__F34DAFE6_26B9_45B2_A16C_88318F51D7C7__INCLUDED_)
//DllMain.cpp文件:
#include "DllMain.h"
#include "windows.h"
#include "stdio.h"
BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
return TRUE; //这里为了更简单的说明,略去了一些代码
}
void __stdcall mtj( long a )
{
MessageBox (0,"mtj function has been called!","Message from the DLL",MB_OK);
}
LPSTR __stdcall tmj( LPCSTR a )
{
MessageBox (NULL,"tmj function has been called!","Message from the DLL",MB_OK);
return a;
}
//EXPORTS.DEF文件:
EXPORTS
mtj @1 PRIVATE
tmj @2 PRIVATE
VB部分:
Private Declare Sub mtj Lib "DynLib1.dll" (ByVal a As Long)
Private Declare Function tmj Lib "DynLib1.dll" (ByVal a As String) As String
Private Sub Form_Click()
Call mtj(1)
MsgBox "Returned String: """ & tmj("text sent to tmj function (parameter 1)") & """"
End Sub
此问题由姚卫军回答。
附加关键字:编程, 源程序, programming, source code, C/C++, MFC, C++ Builder, Borland C++, Turbo C, C, BCB, Visual Basic, VB, 其他方面, 。
|