下面是我写的Dll里面的函数:
===============================
Function GetSoftInfo(Kind: String):String;stdCall
var
    strKind: String;
Begin
    strKind := Kind;
    If strKind='Version' Then
        Result := '系统1。0'
    Else IF strKind='Corporation' Then
            Result := '有限公司'
         Else If strKind='Author' Then
            Result :='设计制作';
End;下面是库的调用
========================================
procedure TfrmMain.FormShow(Sender: TObject);
Type TGetSoftInfo = function(Kind:String):String;StdCall;
var
    LibHandle: THandle;
    SoftInfo: TGetSoftInfo;
begin
    LibHandle := LoadLibrary('SoftInfo.dll');
    If LibHandle = 0 Then
        Begin
        Application.MessageBox('部分程序已毁坏,请重新安装','网站更新程序',MB_OK+MB_ICONERROR);
        Application.Terminate;
        End
    Else
        Begin
        @SoftInfo := GetProcAddress(LibHandle,'GetSoftInfo');
        If @SoftInfo=nil then
            Application.MessageBox('程序信息库出现错误,请立即联系供应商','网站更新程序',MB_OK+MB_ICONERROR)
        Else
            Begin
            frmMain.Caption     := SoftInfo('Version');//出错行
            lblVersion.Caption  := SoftInfo('Version');//出错行
            lblCorp.Caption     := softInfo('Corporation');//出错行
            End;
        FreeLibrary(LibHandle);
        End;
end;
错误提示:
=========
Access violation at address 004042D6 in module 'WebUpdate.exe'. Read of address 01030C0C.
急需帮助,万分感激!

解决方案 »

  1.   

    唉,这个问题都回答不知道多少遍了;
    方法1,用PCHAR不要用STRING;
    方法2,用STRING要在你的USES第一个加SHAREMEM单元,注意,DLL和调用DLL的
    UNIT文件都需要;
      

  2.   

    深有感触,Dll的函数返回值用Windows兼容类型PChar
      

  3.   

    对!如果你的dll是给delphi调用,用shortstring也可以的,如果给vc++等的话,用
    Windows兼容类型PChar比较好的!
    Function GetSoftInfo(Kind: shortString):shortString;stdCall
    var
        strKind: shortString;
    Begin
        strKind := Kind;
        If strKind='Version' Then
            Result := '系统1。0'
        Else IF strKind='Corporation' Then
                Result := '有限公司'
             Else If strKind='Author' Then
                Result :='设计制作';
    End;
    //  用pchar 类型的改写:
    Function GetSoftInfo(Kind: shortString):pchar;stdCall
    var
        strKind: shortString;
    Begin
        strKind := Kind;
        If strKind='Version' Then
            Result := '系统1。0'
        Else IF strKind='Corporation' Then
                Result := '有限公司'
             Else If strKind='Author' Then
                Result :='设计制作';
    End;