C# 如何跨平台调用C++的函数指针!

函数指针搞C++的人应该都知道,效率高,易用性强,隐蔽代码等。在C++里面调用C++写的dll的函数指针那是在容易不过了。使用C#就稍微麻烦点了!那怎么掉呢?通过上面的第一篇文章我们知道应该使用委托 delegate。如果再高级点,定义一个函数指针结构(有点像linux的内核),也同样可以用C#调用。

提示:委托就和C++中的函数指针一样

借用一下别人的列子:在C++的一个标准Win32 api 库ccLic.dll中有一个函数void* WINAPI GetFunctionAddress(unsigned int sn);此函数通过传sn序号得到函数指针即一个函数的地址.之后再通过返回回来的地址进行其它函数的调用

    那么我们必须知道.一个sn号对应的函数结构如 sn=1 -> bool WINAPI CCAskServerLicenseInfo(const char* server_address,unsigned short port,PCcLic_Info plicenseinfo)

在其中 

typedef struct _CcLic_Info {

char ower[64];

unsigned short manage_ip;

unsigned short ramained_ip;

unsigned short useable_time;

unsigned char type;

} CcLic_Info,*PCcLic_Info;

此列的目的就是通过C#调用 CCAskServerLicenseInfo 函数.

 


        [DllImport(

@" ccLic.dll " )]
        
public   static   extern  System.IntPtr Matrix(System.UInt32 sn);//声名入口函数

        //定义函数指针模型
        
public delegate System.Int32 CCAskServerLicenseInfoHandle(System.String servername, System.UInt16 port, System.IntPtr ptr);

        
public static LicenseInfo GetLicentInfo(String server, System.UInt16 port)
        {

            System.IntPtr fPtr = Matrix(1);//获得CCAskServerLicenseInfo地址           CCAskServerLicenseInfoHandle CCAskServerLicenseInfo = Marshal.GetDelegateForFunctionPointer(fPtr, typeof(CCAskServerLicenseInfoHandle)) as CCAskServerLicenseInfoHandle;//将地址转换为C#中的函数指针

            LicenseInfo info = new LicenseInfo();//声名结构并初始化
            IntPtr infoPtr 
= Marshal.AllocCoTaskMem(Marshal.SizeOf(info));//将结构体转换为指针
            CCAskServerLicenseInfo(server, port, infoPtr);//调用函数
            info 
= (LicenseInfo)Marshal.PtrToStructure(infoPtr, typeof(LicenseInfo));//将指针转换为结构体
            
return info;
        }

 [StructLayout(LayoutKind.Sequential, CharSet 
= CharSet.Ansi)]
    
public struct LicenseInfo
    {
        [MarshalAs(UnmanagedType.ByValArray, SizeConst 
= 64)]
        
public System.Char[] ower;
        public System.UInt16 manage_ip; 
        public System.UInt16 ramained_ip;
        public System.UInt16 useable_time; 
        public System.Byte type;
    }

正好项目有个Mobile需要调用,需要用此方式,我试试看行不行.

转载于:https://www.cnblogs.com/qhonge/archive/2008/10/06/1304461.html

CSharp 调用C++ DLL; 参数为指针类型导出函数 c# Csharp调用 c++库 参数为导入和导出指针两种 包含C++ DLL源码 如fun(cont char* A,char*B) A为输入参数,B为输出参数-C# CSharp call C++ DLL lib dll function param use export and import eg: fun(cont char* A,char*B) A IN,B OUT TestDll\Debug\TestCallDll.exe .......\.....\TestCallDll.vshost.exe .......\.....\TestCallDll.vshost.exe.manifest .......\.....\TestDll.dll .......\.....\TestDll.lib .......\TestCallDll\Form1.cs .......\...........\Form1.Designer.cs .......\...........\Form1.resx .......\...........\obj\Debug\TestCallDll.csproj.FileListAbsolute.txt .......\...........\...\.....\TestCallDll.csproj.GenerateResource.Cache .......\...........\...\.....\TestCallDll.exe .......\...........\...\.....\TestCallDll.Form1.resources .......\...........\...\.....\TestCallDll.pdb .......\...........\...\.....\TestCallDll.Properties.Resources.resources .......\...........\Program.cs .......\...........\...perties\AssemblyInfo.cs .......\...........\..........\Resources.Designer.cs .......\...........\..........\Resources.resx .......\...........\..........\Settings.Designer.cs .......\...........\..........\Settings.settings .......\...........\TestCallDll.csproj .......\....Dll\dllmain.cpp .......\.......\ReadMe.txt .......\.......\stdafx.cpp .......\.......\stdafx.h .......\.......\targetver.h .......\.......\TestDll.cpp .......\.......\TestDll.def .......\.......\TestDll.h .......\.......\TestDll.vcproj .......\.......\TestDll.vcproj.PC-201008261742.Administrator.user .......\TestDll.sln .......\TestDll.suo .......\....CallDll\obj\Debug\TempPE .......\...........\...\Debug .......\...........\obj .......\...........\Properties .......\Debug .......\TestCallDll .......\TestDll TestDll
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值