如何使用DELPHI中用C编写的外部DLL

snm*_*snm 3 delphi dll

我需要使用外部dll与数码相机进行通信,我发现程序带有适当的dll,可以实现通信.在dll描述中,我找到了适合我需要的功能.DLL Header看起来像这样....

//-------------------------------------------------------------------
// ReleaseShutter()
// Inputs:
//   timeOutInSecs   timeout in secs to wait for picture to be
//                   taken and downloaded (max 60 secs)
//   pszFilename     option string in which to store the name of the
//                   saved image. Set to NULL if not required
//   numChars        length of pszFilename if defined
//
// Returns://   0 - Success, image saved
//   1 - PSRemote is not running
//   2 - PSRemote is running but camera is not connected
//   3 - Camera is busy
//   4 - Timeout waiting for image to be saved
//   5 - Error releasing shutter
//
// Description:
//   Take a picture and optionally wait for it to be saved to disk.
//
//--------------------------------------------------------------------
PSRemoteLIB_API int __stdcall ReleaseShutter( int timeoutInSecs,
                            char* Filename,int   numChars  );
Run Code Online (Sandbox Code Playgroud)

好的,我加载DLL,使用功能,获取结果状态和外部程序拍照,但我无法获取FILENAME返回!!!! 这是我的代码

procedure TForm1.Button2Click(Sender: TObject);
var   Status: Integer;
Name1: PChar;
DLLHandle: Thandle;
TakePic: Function (T: Integer; Nam: Pchar;Num:Integer):Integer; {$IFDEF WIN32} stdcall; {$ENDIF}

 begin  DLLHandle := LoadLibrary('PSRemoteLib.dll');
   if DLLHandle >= 32 then { success }
     begin   
      Name1:=stralloc(1024);
      TakePic := GetProcAddress(DLLHandle, 'ReleaseShutter');
      Status:=TakePic(60,Name1,SizeOf(Name1));
      label1.Caption:=intTostr(Status);
      label2.Caption:=Name1;
      FreeLibrary(DLLHandle);
     end
   else     MessageDlg('Error: could not find PSRemoteLib.dll', mtError, [mbOk], 0);
  StrDispose(Name1);
end;
Run Code Online (Sandbox Code Playgroud)

我尝试了PChar PWidechar和我在网上找到的所有东西,但没有!

我做错了什么???? 在dll附带的示例.exe中,以cmd模式运行,这很好用!!!! 程序拍照和报告文件名????我有一个示例源代码,看起来像这样

        case 0: // success            if (filename && strlen(filename))            
{
                cout << "Success, image saved as: " << filename << endl;            
}
            else            
{
                cout << "Success, image saved on CF card?" << endl;            
}
            break;
        case 1:            cerr << "PSRemote is not running" << endl;
            break;
        case 2:            cerr << "Camera is not connected" << endl;
            break;
        case 3:            cerr << "Camera is busy" << endl;
            break;
        case 4:            cerr << "Timeout waiting for image to be saved" << endl;
            break;
        default:
            cerr << "ERROR: unexpected return status: " << status << endl;        
}

}
    return nRetCode;
}
Run Code Online (Sandbox Code Playgroud)

请帮助我需要这个!!!

PS也在dll我有类似的功能

{///----------------------------------------------------------------------- }
{/// GetOutputPath() }
{/// Inputs: }
{/// pszPathname string in which to store the pathname of the }
{/// directory currently being used to save images }
{/// numChars length of pszPathname }
{/// }
{/// Returns: }
{/// 0 - Success, pathname returned in pszPathname }
{/// 1 - PSRemote is not running }
{/// 4 - Some other error }
{/// }
{/// Description: }
{/// Returns the full pathname of the directory used for saving images. }
{/// This is the base directory as specified by SetOutputPath() plus }
{/// any separate directories for year, month or day if selected in }
{/// preferences. }
{/// }
{///----------------------------------------------------------------------- }
var
  GetOutputPath: function(pszPathname: PChar; 
                          numChars: var Integer): SREMOTELIB_API INT __STDCALL cdecl  {$IFDEF WIN32} stdcall {$ENDIF}; 
Run Code Online (Sandbox Code Playgroud)

ANd再次获取状态(整数)但不是路径名?????

小智 5

该函数想要获取char缓冲区.这意味着你必须分配这个

Name1 : array[MAX_PATH+1] of AnsiChar;
Run Code Online (Sandbox Code Playgroud)

MAX_PATH在单位Windows中定义,应该足够大.AnsiChar适用于所有Delphi版本,与C++同等char

在调用中,您必须设置指向缓冲区的指针和最大字符数

Status := TakePic(60,Name1,MAX_PATH);
Run Code Online (Sandbox Code Playgroud)