我要在c++中调用delphi编写的DLL中一个函数,而且要传递一字串参数,但不知为什么delphi中显示传过来的字串参数是乱码的,哪位高手遇到过,指点一下,多谢了。
c++代码如下:
typedef int (* pGetData)( char* P1) ;
pGetData GetData;
HINSTANCE hinst;
hinst=LoadLibrary(TEXT("DllTest.dll")); //加载该dll LPCTSTR strContent);
if(hinst==NULL) { MessageBox::Show("Can't load testll.dll");
return;
}
GetData=(pGetData)GetProcAddress(hinst,"TestDll"); //获取导出函数的地址 if(GetData==NULL)
{
MessageBox::Show("Can't get the address of the function");
return;
}
GetData("234"); //运行
FreeLibrary(hinst); //卸载
delphi代码如下:
function TestDll(P1:Pchar):integer; cdecl; begin
ShowMessage(P1);
Result:=8888;
end;
exports TestDll;
begin
end.
c++代码如下:
typedef int (* pGetData)( char* P1) ;
pGetData GetData;
HINSTANCE hinst;
hinst=LoadLibrary(TEXT("DllTest.dll")); //加载该dll LPCTSTR strContent);
if(hinst==NULL) { MessageBox::Show("Can't load testll.dll");
return;
}
GetData=(pGetData)GetProcAddress(hinst,"TestDll"); //获取导出函数的地址 if(GetData==NULL)
{
MessageBox::Show("Can't get the address of the function");
return;
}
GetData("234"); //运行
FreeLibrary(hinst); //卸载
delphi代码如下:
function TestDll(P1:Pchar):integer; cdecl; begin
ShowMessage(P1);
Result:=8888;
end;
exports TestDll;
begin
end.
请问你是用C++调用delphi的DLL吗?
你是怎么定义参数的?能指点一下吗?多谢了。
void CLcView::OnButton1()
{
typedef int (* pGetData)( char* P1) ;
pGetData GetData;
HINSTANCE hinst;
hinst=LoadLibrary(TEXT("DllTest.dll")); //加载该dll LPCTSTR strContent);
if(hinst==NULL) { MessageBox("Can't load testll.dll");
return;
}
GetData=(pGetData)GetProcAddress(hinst,"TestDll"); //获取导出函数的地址 if(GetData==NULL)
{
MessageBox("Can't get the address of the function");
return;
}
GetData("哪有乱码?"); //运行
FreeLibrary(hinst); //卸载
}
请问你是用C++调用delphi的DLL吗?
你是用MFC试的吗???还是c++应用程序,我是用VS2005做的。有QQ吗?想和你交流一下。多谢了
我已经调试很久了,就是会出现乱码,我是用的vs2005的VC++调用delphi的DLL.
你有试过吗??
你是用控制台应用程序,还是窗体应用程序测试的??
delphi中把PChar改成PWideChar试试
我现在就是用的messagebox啊,你用vs2005VC++窗体应用程序测试一下,看有没有乱码??
我是说我现在就是用的messagebox来显示传过来的参数啊,你用vs2005VC++窗体应用程序测试一下,看有没有乱码?? 我用VS2005,用窗体应用程序调用就会出现乱码,用控制台应用程序调用就不会出现乱码。
是的话关注一下,没试过。按理DLL应该用 DELPHI FOR .NET,而且不能用PCHAR。
namespace ttt { using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
using namespace System::Runtime::InteropServices;value class Win32 {
public:
[DllImport("C:\\Documents and Settings\\Administrator\\桌面\\Dll\\Dll\\DllTest.dll")]
static int TestDll(char *);
}; /// <summary>
/// Summary for Form1
///
/// WARNING: If you change the name of this class, you will need to change the
/// 'Resource File Name' property for the managed resource compiler tool
/// associated with all .resx files this class depends on. Otherwise,
/// the designers will not be able to interact properly with localized
/// resources associated with this form.
/// </summary>
public ref class Form1 : public System::Windows::Forms::Form
{
public:
Form1(void)
{
InitializeComponent();
//
//TODO: Add the constructor code here
//
} protected:
/// <summary>
/// Clean up any resources being used.
/// </summary>
~Form1()
{
if (components)
{
delete components;
}
}
private: System::Windows::Forms::Button^ button1;
protected: private:
/// <summary>
/// Required designer variable.
/// </summary>
System::ComponentModel::Container ^components;#pragma region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
void InitializeComponent(void)
{
this->button1 = (gcnew System::Windows::Forms::Button());
this->SuspendLayout();
//
// button1
//
this->button1->Location = System::Drawing::Point(110, 52);
this->button1->Name = L"button1";
this->button1->Size = System::Drawing::Size(75, 23);
this->button1->TabIndex = 0;
this->button1->Text = L"button1";
this->button1->UseVisualStyleBackColor = true;
this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);
//
// Form1
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 12);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(292, 273);
this->Controls->Add(this->button1);
this->Name = L"Form1";
this->Text = L"Form1";
this->ResumeLayout(false); }
#pragma endregion
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) { Win32::TestDll("235"); }
};
}