我用VC寫了一個DLL,Dll主要是把一個字符串,轉換一下。
同時我相在DELPHI中調用DLL中的一個函數C中函數的原形是int code(char *in ,char *out)這個是方由於in與out是指針所以是直接被改變了out的內容我在delphi下面寫了一個測試的程序,但是調用時總出錯!現在找大家幫我看一下錯在哪?unit dll;interface
type
       
Code=function( p,Comeout:Pchar ):Integer; stdcall;
/////////////////////////////////////////////////
//問1:這個地方的Code,要是我原來的C中的函數名嗎?
/////////////////////////////////////////////////
THandle=Integer;
implementationend.
unit maindll;interfaceuses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls,dll;type
  TForm1 = class(TForm)
    Edit1: TEdit;
    Memo1: TMemo;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;var
  Form1: TForm1;implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
var
////////////////////////////////////////////////////////////////
//問2:由於C中的形參是字符串的指針,我這個地方的x,y實參可是以PChar嗎?
//////////////////////////////////////////////////////////////
 
        x:pchar;
        y:pchar;
        Handle:THandle;
        Code1:Code;
        i:integer;
begin
        Handle:=LoadLibrary('encode.dll');
        if Handle <> 0 then
        begin
/////////////////////////////
//問4:GetProcAddress(Handle,'Code');中Code也就是我C裏面的函數名對嗎?
/////////////////////////////
                @Code1:=GetProcAddress(Handle,'Code');
                if @code1 <> nil then
                begin
////////////////////////////////
//這個地方顯示調用成功:
///////////////////////////////
                      ShowMessage('調用了Dll');
////////////////////////////////
//問5:我這樣寫一個字符串可以行嗎?
////////////////////////////////
                     x:='WRyiWnycUsau';
                     y:='';
/////////////////////////////////
//問6:下面這條語句出錯了,請問為什麼我這樣調用函數會出錯,為什麼?
//出現了DEBUG錯。
////////////////////////////////
                     Code1(x,y);
                     edit1.text:=y;
                end //then
                else
                        ShowMessage('调用函数“GetProcAddress”时出错!');
        FreeLibrary(Handle);
        end;
end;
請大家把我這幾個問題分析一下,然後幫助我解決這個問題!謝謝!

解决方案 »

  1.   

    //問1:這個地方的Code,要是我原來的C中的函數名嗎?--不用
    //問2:由於C中的形參是字符串的指針,我這個地方的x,y實參可是以PChar嗎?---可以
    //問4:GetProcAddress(Handle,'Code');中Code也就是我C裏面的函數名對嗎?---是的,注意大小写//問5:我這樣寫一個字符串可以行嗎?
    ////////////////////////////////
                         x:='WRyiWnycUsau';
                         y:='';
    ---不行,你必须为y分配足够的内存。例如:y := StrAlloc( 255 );然后再使用。
      

  2.   

    另,C中函數的原形是int code(char *in ,char *out)也必须是stdcall