c++builder动态调用dll

// 定义
typedef int __stdcall MyFunction (int x, char *str);

int rtn = 0;
String dllName = "XXXX.dll";
HINSTANCE hInstance = LoadLibrary(dllName.c_str());
MyFunction * pMyFunction = (MyFunction*) GetProcAddress(hInstance, "rdcompany"); // 函数名称要正确
if (pMyFunction == NULL)
{
// 提示
}

rtn = pMyFunction(1, "aa");
FreeLibrary(hInstance);
hInstance = NULL;

 

 

C++builder的dll导出类

// Enable RTTI generation for private fields
#pragma explicit_rtti fields(private)
 
class __declspec(delphiclass) TBuffer {
private:
    int wrHead, rdHead;
 
    // ...
public:
    TBuffer() {
 
    }
};

 http://docwiki.embarcadero.com/RADStudio/Berlin/en/Delphi_RTTI_and_C++Builder

 c++导出函数

extern "C" __declspec(dllexport) int __stdcall fun();

静态调用

extern "C" __declspec(dllimport) int __stdcall test();

 

根据dll生成lib文件

cmd命令后

implib E:\MYDEV\CRT_310.lib E:\MYDEV\CRT_310.dll

implib  CRT_310.lib  CRT_310.dll

调用的工程里添加lib文件或者#pragma link "CRT_310.lib"

 这里HANDLE 是typedef void *HANDLE;

typedef HANDLE APIENTRY CommOpenWithBaut(char *Port, BYTE _BaudOption);

 

delphi静态调用dll

function Write_DF02(ucSFI: byte; wFileLen: Word; pucData: PByte): integer; stdcall external 'my.dll';
procedure Set_Path(pchPath: PAnsiChar); stdcall external 'my.dll';

function(var age: integer; var name: double): BOOL; stdcall

 

const
  UrlMonLib = 'URLMON.DLL';

function CreateURLMoniker;                external UrlMonLib name 'CreateURLMoniker';
function readCardInfo_json():PAnsiChar;stdcall; external 'test.dll' ;
function readCardInfo_json():PAnsiChar;stdcall; external 'test.dll' name 'readCardInfo_json_myname';//函数别名


 

 pansichar参数返回

name: array[0..50] of AnsiChar;

age:integer;

money:double;

fun(name,@age,@money);

fun(name,age,money);

 

用 name:TArray<AnsiChar>; 应该也可以。就是麻烦,定义和分配2行代码。

setlength(name,50);

 

二、delphi动态调用dll

 

//函数原型生命
type  
  Taddc = function: TStringList; stdcall; 
  TGetCPUID = function(CPUID: PAnsiChar): integer; stdcall;  //注1    
 // 
var  
  hh: THandle;  
  addc: Taddc;  
  GetId: TGetCPUID; 
  temp: TStringList;  
  i: Integer;  
begin  
  hh := LoadLibrary('DLL.dll');  
  try  
    if hh = 0 then  
 begin
      ShowMessage('load error');
      exit;
    end; //装载方法 @addc := GetProcAddress(hh, PChar('testStr')); GetId := GetProcAddress(hh, pchar('GetCPUID')); //invoke GetId(1); if not (@addc = nil) then begin addc; end else begin RaiseLastWin32Error; end; finally FreeLibrary(hh); end;

 

delphi Berlin版本 如何给PAnsiChar赋值?

    StrCopy(value, PAnsiChar(AnsiString(kvalue)));

因为是UnicodeString,所以必须先进行AnsiString转换。

 

        #define DLLEXPORT_API extern "C" _declspec(dllexport)

dll函数导出 改名别名

delphi太简单了

 

procedure a(); stdcall;
begin

end;

exports
  a name '@$xp$20Controls@TAnchorKind';

function InitCommExt(): Integer; stdcall;

 

c++builder找不到方法

http://toby.logdown.com/posts/220527/c-builder-define-dll-export-function-names-using-def-file

 

DetectCard(ComHandle: THANDLE; var CardType: Byte; var CardInfor: Byte): Integer; stdcall; external 'aa.dll';

dll调用,引用地址

var
  CardType: Byte;
  CardInfor: Byte;
  rec := DetectCard(CHandle, CardType, CardInfor);

 

c#调用dll的函数别名

 [DllImport("test.dll", EntryPoint= "myfun")]  public static extern int myfun_KP(StringBuilder connstr);//

FillChar( readdata, SizeOf( readdata ), 0 );

     var info:tbytes;
                setlength(info,1024);
                     FillChar( info[0], 1024, 0 );

    __cdecl         调用者      从右到左,通过堆栈传递
        __stdcall       函数体      从右到左,通过堆栈传递
        __fastcall      函数体      从右到左,优先使用寄存器(ECX,EDX),然后使用堆栈
        thiscall        函数体      this指针默认通过ECX传递,其它参数从右到左入栈

posted on 2015-11-17 09:43  lypzxy  阅读(820)  评论(0编辑  收藏  举报