DELPI7编写的动态库,在PB中如何声明

c001100c 2010-01-09 12:05:57
DELPI7中 test.dll中的函数如下:
function gf_jmword(as_string: ShortString): wideString; stdcall;
functiongf_word(ac_word: pchar; ac_ts: integer): wideString; stdcall;


上边两个函数想在PB中调用,在PB中如何声明这连个函数,请各位指教。


...全文
244 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
myclife 2010-01-10
  • 打赏
  • 举报
回复
参数传递和返回改用PChar,如果用ShortString可以尝试用下面的函数处理下:


//==============================================================================
// 格式化ShortString
// ShortString类型在第一个字节存放字符串长度(所以其最大255),没有Null结束
//------------------------------------------------------------------------------
procedure FormatShortString(var s:ShortString);
var
li_len : integer;
begin
li_len := length(s);
move(s[1],s[0],li_len);
s[li_len] := #0;
end;
c001100c 2010-01-10
  • 打赏
  • 举报
回复
以下是DELPI下TEST.DLL中的两个函数原型,各位看看有什么问题,如何修改下在PB中才能调用,及在PB中如何声明。特别感谢
library test;

{ Important note about DLL memory management: ShareMem must be the
first unit in your library's USES clause AND your project's (select
Project-View Source) USES clause if your DLL exports any procedures or
functions that pass strings as parameters or function results. This
applies to all strings passed to and from your DLL--even those that
are nested in records and classes. ShareMem is the interface unit to
the BORLNDMM.DLL shared memory manager, which must be deployed along
with your DLL. To avoid using BORLNDMM.DLL, pass string information
using PChar or ShortString parameters. }

uses
SysUtils,
Classes;

{$R *.res}

function gf_jmword(as_string: ShortString): wideString; stdcall;
var
li_i,start_pos:Shortint;
ls_string,old_str,new_str:ShortString;
begin
ls_string := as_string;
for li_i := 0 to 9 do
begin
old_str := IntToStr(li_i);
new_str := char(Ord('a') + li_i);
start_pos := pos(new_str,ls_string);
while start_pos > 0 do
begin
ls_string := StringReplace(ls_string, new_str, old_str, [rfReplaceAll]);
start_pos := pos(ls_string,new_str);
end
end;
Result :=ls_string;
end;


function gf_word(ac_word: pchar; ac_ts: integer): wideString; stdcall;
var
i, start_pos, li_i: integer;
ls_code, ls_limit, old_str, new_str, ls_mid: string;
begin
ls_code := '';
start_pos := 1;
if (SizeOf(ac_word) mod 2) = 0 then
begin
for i := 1 to (SizeOf(ac_word) div 2) do
begin
if ls_code = '' then
ls_code := FormatFloat('00', ord(ac_word[i * 2 - 1]) - 160) + FormatFloat('00', ord(ac_word[i * 2]) - 1600)
else
ls_code := ls_code + FormatFloat('00', ord(ac_word[i * 2 - 1]) - 1600) + FormatFloat('00', ord(ac_word[i * 2]) - 1600)
end;

ls_limit := FormatDateTime('yyyymmdd', Date + ac_ts);
for li_i := 0 to 9 do
begin
old_str := IntToStr(li_i);
new_str := char(ord('a') + li_i);
start_pos := Pos(old_str,ls_limit);
while start_pos > 0 do
begin
ls_limit := StringReplace(ls_limit, old_str, new_str,[rfReplaceAll]);
start_pos := Pos(old_str,ls_limit);
end;
end;
ls_code := ls_code + ls_limit;
end;
Result := ls_code;
end;

exports
gf_jmword,
gf_word;

begin

end.
dawugui 2010-01-09
  • 打赏
  • 举报
回复
拷贝一些调用方法,你参考:
FUNCTION ulong AbortDoc(ulong hdc) LIBRARY "gdi32.dll"
FUNCTION ulong AbortPath(ulong hdc) LIBRARY "gdi32.dll"
FUNCTION ulong AbortPrinter(ulong hPrinter) LIBRARY "winspool.drv"
FUNCTION ulong AbortSystemShutdown(ref string lpMachineName) LIBRARY "advapi32.dll" ALIAS FOR "AbortSystemShutdownA"
FUNCTION ulong AccessCheck(ref SECURITY_DESCRIPTOR pSecurityDescriptor,ulong ClientToken,ulong DesiredAccess,ref GENERIC_MAPPING GenericMapping,ref PRIVILEGE_SET PrivilegeSet,ref ulong PrivilegeSetLength,ref ulong GrantedAccess,ulong Status) LIBRARY "advapi32.dll"
FUNCTION ulong AccessCheckAndAuditAlarm(ref string SubsystemName,ref Any HandleId,ref string ObjectTypeName,ref string ObjectName,ref SECURITY_DESCRIPTOR SecurityDescriptor,ulong DesiredAccess,ref GENERIC_MAPPING GenericMapping,ulong ObjectCreation,ref ulong GrantedAccess,ulong AccessStatus,ulong pfGenerateOnClose) LIBRARY "advapi32.dll" ALIAS FOR "AccessCheckAndAuditAlarmA"
FUNCTION ulong ActivateKeyboardLayout(ulong HKL,ulong flags) LIBRARY "user32.dll"
FUNCTION ulong AddAccessAllowedAce(ref ACL pAcl,ulong dwAceRevision,ulong AccessMask,ref Any pSid) LIBRARY "advapi32.dll"
FUNCTION ulong AddAccessDeniedAce(ref ACL pAcl,ulong dwAceRevision,ulong AccessMask,ref Any pSid) LIBRARY "advapi32.dll"
FUNCTION ulong AddAce(ref ACL pAcl,ulong dwAceRevision,ulong dwStartingAceIndex,ref Any pAceList,ulong nAceListLength) LIBRARY "advapi32.dll"
FUNCTION Integer AddAtom(ref string lpString) LIBRARY "kernel32.dll" ALIAS FOR "AddAtomA"
FUNCTION ulong AddAuditAccessAce(ref ACL pAcl,ulong dwAceRevision,ulong dwAccessMask,ref Any pSid,ulong bAuditSuccess,ulong bAuditFailure) LIBRARY "advapi32.dll"
FUNCTION ulong AddFontResource(ref string lpFileName) LIBRARY "gdi32.dll" ALIAS FOR "AddFontResourceA"
FUNCTION ulong AddForm(ulong hPrinter,ulong Level,ref Byte pForm) LIBRARY "winspool.drv" ALIAS FOR "AddFormA"
FUNCTION ulong AddJob(ulong hPrinter,ulong Level,ref Byte pData,ulong cdBuf,ref ulong pcbNeeded) LIBRARY "winspool.drv" ALIAS FOR "AddJobA"
FUNCTION ulong AddMonitor(ref string pName,ulong Level,ref Byte pMonitors) LIBRARY "winspool.drv" ALIAS FOR "AddMonitorA"
FUNCTION ulong AddPort(ref string pName,ulong hwnd,ref string pMonitorName) LIBRARY "winspool.drv" ALIAS FOR "AddPortA"
FUNCTION ulong AddPrinter(ref string pName,ulong Level,ref Byte pPrinter) LIBRARY "winspool.drv" ALIAS FOR "AddPrinterA"
FUNCTION ulong AddPrinterConnection(ref string pName) LIBRARY "winspool.drv" ALIAS FOR "AddPrinterConnectionA"
FUNCTION ulong AddPrinterDriver(ref string pName,ulong Level,ref Byte pDriverInfor) LIBRARY "winspool.drv" ALIAS FOR "AddPrinterDriverA"
FUNCTION ulong AddPrintProcessor(ref string pName,ref string pEnvironment,ref string pPathName,ref string pPrintProcessorName) LIBRARY "winspool.drv" ALIAS FOR "AddPrintProcessorA"
FUNCTION ulong AddPrintProvidor(ref string pName,ulong Level,ref Byte pProvidorInfo) LIBRARY "winspool.drv" ALIAS FOR "AddPrintProvidorA"
FUNCTION ulong AdjustTokenGroups(ulong TokenHandle,ulong ResetToDefault,ref TOKEN_GROUPS NewState,ulong BufferLength,ref TOKEN_GROUPS PreviousState,ref ulong ReturnLength) LIBRARY "advapi32.dll"
FUNCTION ulong AdjustTokenPrivileges(ulong TokenHandle,ulong DisableAllPrivileges,ref TOKEN_PRIVILEGES NewState,ulong BufferLength,ref TOKEN_PRIVILEGES PreviousState,ref ulong ReturnLength) LIBRARY "advapi32.dll"
FUNCTION ulong AdjustWindowRect(ref RECT lpRect,ulong dwStyle,ulong bMenu) LIBRARY "user32.dll" ALIAS FOR "AdjustWindowRectA"
FUNCTION ulong AdjustWindowRectEx(ref RECT lpRect,ulong dsStyle,ulong bMenu,ulong dwEsStyle) LIBRARY "user32.dll"
FUNCTION ulong AdvancedDocumentProperties(ulong hwnd,ulong hPrinter,ref string pDeviceName,ref DEVMODE pDevModeOutput,ref DEVMODE pDevModeInput) LIBRARY "winspool.drv" ALIAS FOR "AdvancedDocumentPropertiesA"
FUNCTION ulong AllocateAndInitializeSid(ref SID_IDENTIFIER_AUTHORITY pIdentifierAuthority,Byte nSubAuthorityCount,ulong nSubAuthority0,ulong nSubAuthority1,ulong nSubAuthority2,ulong nSubAuthority3,ulong nSubAuthority4,ulong nSubAuthority5,ulong nSubAuthority6,ulong nSubAuthority7,ref ulong lpPSid) LIBRARY "advapi32.dll"
FUNCTION ulong AllocateLocallyUniqueId(ref LARGE_INTEGER Luid) LIBRARY "advapi32.dll"
FUNCTION ulong AllocConsole() LIBRARY "kernel32.dll"
FUNCTION ulong AngleArc(ulong hdc,ulong x,ulong y,ulong dwRadius,Double eStartAngle,Double eSweepAngle) LIBRARY "gdi32.dll"
FUNCTION ulong AnimatePalette(ulong hPalette,ulong wStartIndex,ulong wNumEntries,ref PALETTEENTRY lpPaletteColors) LIBRARY "gdi32.dll" ALIAS FOR "AnimatePaletteA"
FUNCTION ulong AnyPopup() LIBRARY "user32.dll"
FUNCTION ulong AppendMenu(ulong hMenu,ulong wFlags,ulong wIDNewItem,ref string lpNewItem) LIBRARY "user32.dll" ALIAS FOR "AppendMenuA"
FUNCTION ulong Arc(ulong hdc,ulong X1,ulong Y1,ulong X2,ulong Y2,ulong X3,ulong Y3,ulong X4,ulong Y4) LIBRARY "gdi32.dll"
FUNCTION ulong ArcTo(ulong hdc,ulong X1,ulong Y1,ulong X2,ulong Y2,ulong X3,ulong Y3,ulong X4,ulong Y4) LIBRARY "gdi32.dll"
FUNCTION ulong AreAllAccessesGranted(ulong GrantedAccess,ulong DesiredAccess) LIBRARY "advapi32.dll"
FUNCTION ulong AreAnyAccessesGranted(ulong GrantedAccess,ulong DesiredAccess) LIBRARY "advapi32.dll"
FUNCTION ulong ArrangeIconicWindows(ulong hwnd) LIBRARY "user32.dll"
FUNCTION ulong AttachThreadInput(ulong idAttach,ulong idAttachTo,ulong fAttach) LIBRARY "user32.dll"
dawugui 2010-01-09
  • 打赏
  • 举报
回复
看不懂你的类型.

你下个PB的API大全,看看就知道了.




API调用规范及方法













(独孤求败二零零四年一月二十六日整理于深圳市南山区)



外部函数就是用其他语言编写并储存在动态链接库里面的函数。这些动态链接库可以存放多个外部函数,外部函数可以是用VC++、Delphi等其他编程语言编写的。但是无论用什么语言编写,它都要符合基本的原则,就是必须用Pascal(16位)或标准(32位)调用规则编写,否则在PB中将无法调用。
PB中调用外部函数的语法是:
{access} FUNCTION returndatatype FunctionName ( { { REF} datatype1 arg1, …, {REF} datatypen argn}) LIBRARY “libname” ALLAS FOR “extname”
或:{access} SUBROUTINE SubroutineName ( { {REF} datatype1 arg1, … ,{REF} datatypen argn })
LIBRARY “libname” ALLAS FOR “extname”
由于编写语言的不同,所以肯定会有数据类型不匹配的问题,下表列出了C++和PB数据类型的转换对应关系:
C++数据类型 PB数据类型
Byte,Char Char
Bool Boolean
Char Blob
Char* String Ref
Double Double
Dword Ulong
Float Real
Handle Uint
Hwnd Ulong
Int Integer
Long Long
Lpbyte String Ref
Lpdword Ulong
Lpint Integer
Lplong Long
Lpstr String Ref
Lpviod String
Lpword Uint
Ulong/unsigned long Ulong
Unsigned int/Unsigned Short/Word Uint

pb8 2010-01-09
  • 打赏
  • 举报
回复
delphi的pchar就是传址的,可以用ref string或 ref char xx[255],如果用ref string的话必须在调用前分配内存空间,方式为
x = space(255)
gf_word(x,0)
pb8 2010-01-09
  • 打赏
  • 举报
回复
function string gf_jmword(string as_string) library 'test.dll'
function string gf_word(ref string ac_word,long ac_ts) library 'test.dll'
记得这样传进去的话是采用的ansi方式,在delphi中有时候需要转换成unicode的
pb8 2010-01-09
  • 打赏
  • 举报
回复
地址引用的时候使用ref方式,就是该变量是传址
c001100c 2010-01-09
  • 打赏
  • 举报
回复
{access} FUNCTION returndatatype FunctionName ( { { REF} datatype1 arg1, …, {REF} datatypen argn}) LIBRARY “libname” ALLAS FOR “extname”


这段中的REF代表什么意思
c001100c 2010-01-09
  • 打赏
  • 举报
回复
实在是不会转会,请各位在帮助意下吧
myclife 2010-01-09
  • 打赏
  • 举报
回复
DELPHI的shortstring、string等类型不能直接作为函数的返回值和参数给PB用。

680

社区成员

发帖
与我相关
我的任务
社区描述
PowerBuilder API 调用
社区管理员
  • API 调用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧