我要在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. 

解决方案 »

  1.   

    TO    FierceLC 
    请问你是用C++调用delphi的DLL吗?
    你是怎么定义参数的?能指点一下吗?多谢了。
      

  2.   

    我试了一下你的函数,没有乱码啊:
    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);  //卸载
     
    }
      

  3.   

    TO    FierceLC 
    请问你是用C++调用delphi的DLL吗? 
    你是用MFC试的吗???还是c++应用程序,我是用VS2005做的。有QQ吗?想和你交流一下。多谢了
      

  4.   

    我用vc6.0做的,mfc调用Delphi7 的dll, qq 412790129
      

  5.   

    to   chenyq2008 
    我已经调试很久了,就是会出现乱码,我是用的vs2005的VC++调用delphi的DLL.
    你有试过吗??
      

  6.   

    VC6,VS2005,VS2008都测试过,没有任何问题(结果都正常).
      

  7.   

    TO  unsigned 
    你是用控制台应用程序,还是窗体应用程序测试的??
      

  8.   

    yi ban yong stdcall  ba
      

  9.   

    你把参数char* P1改成w_char *
    delphi中把PChar改成PWideChar试试
      

  10.   

    不过,有一个建议,在你的DLL当中,最好不要使用ShowMessage,而换用MessageBox或者自己定义的窗体,尽可能少用与Application相关的东西.
      

  11.   

    to    unsigned 
    我现在就是用的messagebox啊,你用vs2005VC++窗体应用程序测试一下,看有没有乱码?? 
      

  12.   

    不明白你说的是啥,把你的程序都打包发到我邮箱[email protected],有空帮你看看.
      

  13.   

    to    unsigned 
    我是说我现在就是用的messagebox来显示传过来的参数啊,你用vs2005VC++窗体应用程序测试一下,看有没有乱码?? 我用VS2005,用窗体应用程序调用就会出现乱码,用控制台应用程序调用就不会出现乱码。
      

  14.   

    你用的CLI代码调用DLL存在问题,不过很抱歉,我帮不了你.
      

  15.   

    另外Managed Code调用的DLL,最好是使用stdcall,否则相当麻烦
      

  16.   

    楼主是不是C++用.NET,delphi的DLL用WIN32?
    是的话关注一下,没试过。按理DLL应该用 DELPHI FOR .NET,而且不能用PCHAR。
      

  17.   

    重新改过的代码:#pragma once
    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");  }
    };
    }
      

  18.   

    记住,DLL要用stdcall,而不是先前的cdecl.