调用外部DLL,如:DelphiC++Java的DLL的调用方法,给出代码片段?

//指示该属性化方法由非托管动态链接库 (DLL) 作为静态入口点公开。

System.Runtime.InteropServices.DllImportAttribute

有时需要写上路径的如[DllImport(@"C:\OJ\Bin\Judge.dll")]这样指定DLL的绝对路径就可以正常装载。

假如没有路径的话,DllImport会按照顺序自动去寻找的地方:

     1、exe所在目录      2、System32目录      3、环境变量目录

²  Delphi_未测试

[ DllImport ( "WZFSE.dll" , CharSet = CharSet.Ansi , 
        CallingConvention = CallingConvention.StdCall )]
public static extern void InitDll(IntPtr handle, bool methodAddress);


第一个参数是指要引用DLL的名字, 这个名字应该是个常量(否则会出错)。

CharSet 参数指示用在入口点中的字符集。如果未指定 CharSet,则使用默认值 CharSet.Auto。

CallingConvention 参数指示入口点的调用约定。如果未指定 CallingConvention,则使用默认值 CallingConvention.Winapi。

 

写一个函数就相应的应用起对应的DLL。

[DllImport ( "user32.dll" , CharSet = CharSet.Ansi , 
         CallingConvention = CallingConvention.StdCall )]
public static extern void MoveWindow ( IntPtr handler , int x , int y , 
         int width , int height , bool repaint );


如何将Dephi的窗体显示在自己的页面中(且不能显示Delphi窗体的标题栏,实现无缝的结合)。

http://www.cnblogs.com/elivn/archive/2010/11/19/1881686.html

Delphi一般类型对应如下:

Dephi-->C#    

intger -->int    longint -->long  

pchar -->string  THandle -->IntPtr

Char[]-->string

例:

Delphi DLL中的方法:

function GetXMLByNet(piChartType: integer; psXMLFileName: PChar; psPriChartFileName: PChar; psSecChartFileName: PChar; out psPriHotMsg: PChar; out psSecHotMsg: PChar): integer; stdcall;export;

    private class DrawChartFromDll
    {
        private const string _fileDll = @"Chart.dll";  //定义DLL文件名,此文件路径要加到系统Path中        
        [DllImport ( _fileDll , EntryPoint = "GetXMLByNet" , CharSet = CharSet.Ansi , 
          CallingConvention =CallingConvention.StdCall )] 
//调用非托管Dll,
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
library fundll; { 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 ShareMem, // ShareMem 一定放第一个 Windows, Messages, SysUtils, math, strutils, registry , StdCtrls, ExtCtrls, ADODB, DB,dateutils,Dialogs; // fucs in 'fucs.pas'; const INPASSSTR='89ABCDEFGcdefghijkHIJ%^KLMN0123opqrstuOP -_\|/?@#$&*' ; //切记:Library 的名字大小写没关系,可是DLL-Func的大小写就有关系了。 // 在 DLL-Func-Name写成MyMax与myMAX是不同的。如果写错了,立即的结果是你调用到此DLL的AP根本开不起来。 //参数的大小写就没关系了。甚至不必同名。如原型中是 (X,Y:integer)但引用时写成(A,B:integer),那是没关系的。 //切记:要再加个stdcall。书上讲,如果你是用DelphiDLL,且希望不仅给 Delphi-AP也希望BCB/VC-AP等使用的话,那你最好加个Stdcall ; //参数型态:Delphi有很多种它自己的变量型态,这些当然不是DLL所喜欢的,Windows/DLL的母语应该是C。所以如果要传进传出DLL的参数,我们尽可能照规矩来用。这两者写起来,后者会麻烦不少。如果你对C不熟的话,那也没关系。我们以后再讲。 //3.将这些可共享的Func送出DLL,让外界﹝就是你的Delphi-AP啦﹞使用: //光如此,你的AP还不能用到这些,你还要加个Exports才行。 代码: //=============比较大小的函数=============== Function MyMax ( X , Y : integer ) : integer ; stdcall ; //stdcall 可以让 BCB/VC-AP等使用的 begin if X > Y then Result := X else Result := Y ; end ; //==============加密======================= function Inpass(s:string):string; stdcall ; var i:integer; passstr,dd:string; begin for i:=1 to length(s) do begin dd:=inttohex(ansipos(s[i],inpassstr),4); if dd='0000' then begin result:='0';exit end; passstr:=passstr+dd ; end; Result :=passstr; end; //==============解密======================= function Outpass(s:string):string;stdcall ; var pass,dd:string; i,leng:integer; begin leng:= floor(length(s)/4); pass:=''; for i:=1 to leng do begin dd:=ansimidstr(s,(i-1)*4+1,4); if strtoint('$'+dd)=0 then begin result:='0';exit;end; if strtoint('$'+dd)>78 then begin result:='0'; exit end; pass:=pass+ansimidstr(inpassstr,strtoint('$'+dd),1) ; end; Result :=pass ; end; //==========test========================= function jsjyh(strym:string):string;stdcall; var newstr1,he,oldstr:string; tj:boolean; i:integer; begin i:=1; he:=''; tj:=true; // 取出要参与校验和计算的字符串给oldstr if (length(strym) mod 2)0 then begin showmessage('你输入的源码个数有错,不能是奇数个,请重输入!'); exit; end; oldstr:=trim(strym); while tj=true do begin newstr1:=copy(oldstr,i,2); oldstr:=copy(oldstr,i+2,length(oldstr)-2); //开始计算校验和并给he变量 if he='' then begin he:=inttohex(strtointdef('$'+newstr1,16)+ strtointdef('$'+'00',16),2); he:=rightstr(he,2); end else begin he:=inttohex(strtointdef('$'+newstr1,16)+ strtointdef('$'+he,16),2); he:=rightstr(he,2); end; if length(oldstr) =0 then tj:=false; end; result:=strym+he; end; //============================================== {$R *.RES} //将这些可共享的Func送出DLL,让外界﹝就是你的Delphi-AP啦﹞使用: //光如此,你的AP还不能用到这些,你还要加个Exports才行。 代码: exports MyMax,Inpass,Outpass,jsjyh; begin end.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值