Creating the ReturnString function The ReturnString function merits some discussion. Any string that you want to pass back to APW must go through this function. What it does is allocate some memory for the string, copy the string into it, and then return it to APW. Note that APW does the freeing of the memory allocated. I will make some more comments when creating the UCase function. This function should not be exported; it will not be called directly from APW.Open the Unit1 code window. First we need to declare our function. In the 'interface' section, after the line that declares the Add function, add this line:function ReturnString(StringIn: Pchar): Pchar ; far;There is no need to use the 'stdcall' directive on this function. The 'stdcall' is only necessary on functions that are to be exported. Your interface section should look like thisuses sysutils, wintypes, winprocs; function Add( n1, n2 : Integer ) : Integer ; export; {$ifdef WIN32} stdcall ; {$endif} function ReturnString(StringIn: Pchar): Pchar ; far; Here is the ReturnString function. It is placed in the 'implementation' section, after the Add function.function ReturnString(StringIn: Pchar): Pchar ; { Puts a string on the windows heap and returns a windows handle to the string } var hGlobalMemory: THandle; lpGlobalMemory: pointer ; begin {This line sets up a character pointer from windows heap } hGlobalMemory := GlobalAlloc(GMEM_MOVEABLE , strlen(StringIn)+1); if hGlobalMemory <> 0 then { memory allocated OK } begin { This sets up the string in the memory grabbed from the heap } lpGlobalMemory := GlobalLock(hGlobalMemory); lstrcpy(lpGlobalMemory,StringIn); GlobalUnlock(hGlobalMemory); end; { This returns a pointer to the string to APW } ReturnString := Pchar(hGlobalMemory); end;
unit myunit; interface uses sharemem, Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, Buttons, imm; type timmdata = array of pchar; {------------声明}function getallime(): timmdata ; export; {$ifdef WIN32} stdcall; {$endif} function ReturnString(StringIn: Pchar): Pchar ; far; function opencid(cidname:pchar):BOOLEAN; export; {$ifdef WIN32} stdcall; {$endif}implementation //获取所有输入发的数组 function getallime(): timmdata ; stdcall; var j: integer; a: timmdata; retstr : array[0..64] of char; Begin SetLength(a,screen.Imes.Count); for j:=0 to screen.Imes.Count-1 do begin // a[j]:=StrAlloc(Length(screen.imes.strings[j])+1); // a[j]:=pchar(screen.imes.strings[j]); StrPCopy(retstr,screen.imes.strings[j]); { 把一个 pascal 类型的串转换到 C 类型} a[j]:= ReturnString( retstr ); {把转换的结果送到Authorware} end; result:=a; SetLength(a,0); End; function ReturnString(StringIn: Pchar): Pchar ; { 把一个串放到Windows的堆栈中,把Windows的句柄放到这个串} var hGlobalMemory: THandle; lpGlobalMemory: pointer ; begin {从Windows的堆栈中设置一个字符串指针 } hGlobalMemory := GlobalAlloc(GMEM_MOVEABLE , strlen(StringIn)+1); if hGlobalMemory <> 0 then { 内存分配正常 } begin { 在堆栈当中已经占用的内存放置该串 } lpGlobalMemory := GlobalLock(hGlobalMemory); lstrcpy(lpGlobalMemory,StringIn); GlobalUnlock(hGlobalMemory); end; { 把这个串的指针返回到Authorware } ReturnString := Pchar(hGlobalMemory); end;//打开输入法的函数 function opencid(cidname: pchar) : BOOLEAN; stdcall; var I: Integer; mycid:hkl; instr: string[64]; { 一些中间结果串 } retstr : array[0..64] of char; { 返回到Authorware的串} begin instr := Strpas(cidname); StrPCopy(retstr,instr); I:=screen.Imes.Indexof(retstr); mycid:=hkl(screen.Imes.Objects[i]); activatekeyboardlayout(mycid, KLF_activate); RESULT:=TRUE; end; end.以上在Delphi 都不能通过了(出现的是乱码)!请问大侠哪里有误?
昏倒...不过我现在这里只有C++Builder的代码,Delphi的没有了,也没有Authorware。返回值不能使用你的那个Array of Char,直接使用PCHar.
AW7当中的说明: Custom functions in DLLs Windows dynamic link libraries (DLLs) provide a way of linking to external functions that you call from within calculation icons. Authorware must have the following information about a function in a DLL before it can load it: • Where the function resides • The function’s name • How many and which type of arguments the function takes • The type of arguments the function returns
The ReturnString function merits some discussion. Any string that you want to pass back to APW must go through this function. What it does is allocate some memory for the string, copy the string into it, and then return it to APW. Note that APW does the freeing of the memory allocated. I will make some more comments when creating the UCase function. This function should not be exported; it will not be called directly from APW.Open the Unit1 code window.
First we need to declare our function. In the 'interface' section, after the line that declares the Add function, add this line:function ReturnString(StringIn: Pchar): Pchar ; far;There is no need to use the 'stdcall' directive on this function. The 'stdcall' is only necessary on functions that are to be exported. Your interface section should look like thisuses sysutils, wintypes, winprocs; function Add( n1, n2 : Integer ) : Integer ; export; {$ifdef WIN32} stdcall ; {$endif}
function ReturnString(StringIn: Pchar): Pchar ; far; Here is the ReturnString function. It is placed in the 'implementation' section, after the Add function.function ReturnString(StringIn: Pchar): Pchar ;
{ Puts a string on the windows heap and returns a windows handle to the string }
var
hGlobalMemory: THandle;
lpGlobalMemory: pointer ; begin {This line sets up a character pointer from windows heap }
hGlobalMemory := GlobalAlloc(GMEM_MOVEABLE , strlen(StringIn)+1);
if hGlobalMemory <> 0 then { memory allocated OK }
begin { This sets up the string in the memory grabbed from the heap }
lpGlobalMemory := GlobalLock(hGlobalMemory);
lstrcpy(lpGlobalMemory,StringIn);
GlobalUnlock(hGlobalMemory);
end; { This returns a pointer to the string to APW }
ReturnString := Pchar(hGlobalMemory); end;
interface
uses
sharemem, Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Buttons, imm;
type
timmdata = array of pchar; {------------声明}function getallime(): timmdata ; export; {$ifdef WIN32} stdcall; {$endif}
function ReturnString(StringIn: Pchar): Pchar ; far;
function opencid(cidname:pchar):BOOLEAN; export; {$ifdef WIN32} stdcall; {$endif}implementation //获取所有输入发的数组
function getallime(): timmdata ; stdcall;
var
j: integer;
a: timmdata;
retstr : array[0..64] of char;
Begin
SetLength(a,screen.Imes.Count);
for j:=0 to screen.Imes.Count-1 do
begin
// a[j]:=StrAlloc(Length(screen.imes.strings[j])+1);
// a[j]:=pchar(screen.imes.strings[j]);
StrPCopy(retstr,screen.imes.strings[j]); { 把一个 pascal 类型的串转换到 C 类型}
a[j]:= ReturnString( retstr ); {把转换的结果送到Authorware}
end;
result:=a;
SetLength(a,0);
End;
function ReturnString(StringIn: Pchar): Pchar ;
{ 把一个串放到Windows的堆栈中,把Windows的句柄放到这个串}
var
hGlobalMemory: THandle;
lpGlobalMemory: pointer ;
begin
{从Windows的堆栈中设置一个字符串指针 }
hGlobalMemory := GlobalAlloc(GMEM_MOVEABLE , strlen(StringIn)+1);
if hGlobalMemory <> 0 then { 内存分配正常 }
begin
{ 在堆栈当中已经占用的内存放置该串 }
lpGlobalMemory := GlobalLock(hGlobalMemory);
lstrcpy(lpGlobalMemory,StringIn);
GlobalUnlock(hGlobalMemory);
end;
{ 把这个串的指针返回到Authorware }
ReturnString := Pchar(hGlobalMemory);
end;//打开输入法的函数
function opencid(cidname: pchar) : BOOLEAN; stdcall;
var
I: Integer;
mycid:hkl;
instr: string[64]; { 一些中间结果串 }
retstr : array[0..64] of char; { 返回到Authorware的串}
begin
instr := Strpas(cidname);
StrPCopy(retstr,instr);
I:=screen.Imes.Indexof(retstr);
mycid:=hkl(screen.Imes.Objects[i]);
activatekeyboardlayout(mycid, KLF_activate);
RESULT:=TRUE;
end;
end.以上在Delphi 都不能通过了(出现的是乱码)!请问大侠哪里有误?
Custom functions in DLLs
Windows dynamic link libraries (DLLs) provide a way of linking to external functions that you
call from within calculation icons. Authorware must have the following information about a
function in a DLL before it can load it:
• Where the function resides
• The function’s name
• How many and which type of arguments the function takes
• The type of arguments the function returns
用的是AW7
"标准的DLL是不建议使用PChar作为返回值的。如果现行版本的AW支持标准的DLL调用,那么应该就可以通过输出参数来返回结果。",是这样吗?我看到有篇文章说得和C语言兼容需要PCHAR,我才用PCHAR的。请问输出参数和返回结果一般都应该是什么数据类型呢?
谢谢啦