当前位置: 首页 > news >正文

遂宁市网站建设_网站建设公司_移动端适配_seo优化

用cms创建自己带数据库的网站和在本机搭建网站运行平台的心得体会,ip138查询网站网址域名ip,用树莓派做网站,岑巩网站建设在最近的项目中#xff0c;牵涉到项目源代码保密问题#xff0c;由于代码是C#写的#xff0c;容易被反编译#xff0c;因此决定抽取核心算法部分使用C编写#xff0c;C到目前为止好像还不能被很好的反编译#xff0c;当然如果你是反汇编高手的话#xff0c;也许还是有可…在最近的项目中牵涉到项目源代码保密问题由于代码是C#写的容易被反编译因此决定抽取核心算法部分使用C编写C到目前为止好像还不能被很好的反编译当然如果你是反汇编高手的话也许还是有可能反编译。这样一来就涉及C#托管代码与C非托管代码互相调用于是调查了一些资料顺便与大家分享一下: 一. C# 中静态调用C动态链接       1. 建立VC工程CppDemo建立的时候选择Win32 Console(dll)选择Dll。     2. 在DllDemo.cpp文件中添加这些代码。 Codeextern C __declspec(dllexport) int Add(int a,int b){         return ab;}     3. 编译工程。     4. 建立新的C#工程选择Console应用程序建立测试程序InteropDemo    5. 在Program.cs中添加引用using System.Runtime.InteropServices;     6. 在pulic class Program添加如下代码    Codeusing System;using System.Collections.Generic;using System.Text;using System.Runtime.InteropServices;namespace InteropDemo{    class Program    {        [DllImport(CppDemo.dll, EntryPoint  Add, ExactSpelling  false, CallingConvention  CallingConvention.Cdecl)]        public static extern int Add(int a, int b); //DllImport请参照MSDN        static void Main(string[] args)        {            Console.WriteLine(Add(1, 2));            Console.Read();        }    }}    好了现在您可以测试Add程序了是不是可以在C# 中调用C动态链接了当然这是静态调用需要将CppDemo编译生成的Dll放在DllDemo程序的Bin目录下 二. C# 中动态调用C动态链接  在第一节中讲了静态调用C动态链接由于Dll路径的限制使用的不是很方便C#中我们经常通过配置动态的调用托管Dll例如常用的一些设计模式Abstract Factory Provider Strategy模式等等那么是不是也可以这样动态调用C动态链接呢只要您还记得在C中通过LoadLibrary, GetProcess, FreeLibrary这几个函数是可以动态调用动态链接的它们包含在kernel32.dll中那么问题迎刃而解了下面我们一步一步实验     1.  将kernel32中的几个方法封装成本地调用类NativeMethod Codeusing System;using System.Collections.Generic;using System.Text;using System.Runtime.InteropServices;namespace InteropDemo{    public static class NativeMethod    {        [DllImport(kernel32.dll, EntryPoint  LoadLibrary)]        public static extern int LoadLibrary(            [MarshalAs(UnmanagedType.LPStr)] string lpLibFileName);        [DllImport(kernel32.dll, EntryPoint  GetProcAddress)]        public static extern IntPtr GetProcAddress(int hModule,            [MarshalAs(UnmanagedType.LPStr)] string lpProcName);        [DllImport(kernel32.dll, EntryPoint  FreeLibrary)]        public static extern bool FreeLibrary(int hModule);    }}       2. 使用NativeMethod类动态读取CDll获得函数指针并且将指针封装成C#中的委托。原因很简单C#中已经不能使用指针了如下                      int hModule NativeMethod.LoadLibrary(c:CppDemo.dll);             IntPtr intPtr NativeMethod.GetProcAddress(hModule, Add); 详细请参见代码 Codeusing System;using System.Collections.Generic;using System.Text;using System.Runtime.InteropServices;namespace InteropDemo{    class Program    {        //[DllImport(CppDemo.dll, EntryPoint  Add, ExactSpelling  false, CallingConvention  CallingConvention.Cdecl)]        //public static extern int Add(int a, int b); //DllImport请参照MSDN        static void Main(string[] args)        {            //1. 动态加载C Dll            int hModule  NativeMethod.LoadLibrary(c:\CppDemo.dll);            if (hModule  0) return;            //2. 读取函数指针            IntPtr intPtr  NativeMethod.GetProcAddress(hModule, Add);            //3. 将函数指针封装成委托            Add addFunction  (Add)Marshal.GetDelegateForFunctionPointer(intPtr, typeof(Add));            //4. 测试            Console.WriteLine(addFunction(1, 2));            Console.Read();        }        /// summary        /// 函数指针        /// /summary        /// param namea/param        /// param nameb/param        /// returns/returns        delegate int Add(int a, int b);    }}       通过如上两个例子我们可以在C#中动态或者静态的调用C写的代码了. 转自http://www.cnblogs.com/Jianchidaodi/archive/2009/03/09/1407270.html  转载于:https://www.cnblogs.com/newstart/archive/2012/10/24/2736780.html
http://www.ihoyoo.com/news/140878.html

相关文章:

  • 盱眙县建设局网站手机 wordpress
  • 做软件营销网站怎么样襄阳门户网站建设
  • 用jsp做的网站需要什么工具栏温州如何进行网站推广
  • Wordpress大前端破解版seo优
  • 做网站怎么存放视频免费发布信息网站大全666
  • 小公司做网站需要注意什么网站开发背景图片
  • 帮做ppt网站揭阳专业做网站
  • 网站建设和网站推广可以同一家做吗简述网络营销的方法
  • 博宇娱乐网站建设网站开发全程实例
  • 软件免费网站大全我是做环保类产品注册哪些浏览量大的网站推销自己的产品比较好呢
  • 东莞塘厦做网站建站公司怎么备案
  • cco网站素材做网站优化的教程
  • 烟台网站建设方案推广wordpress登录及注册
  • 专做婴儿的网站wordpress返回上一个页面
  • 高端网站开发设计简介东莞专业做外贸网站的公司
  • 做网站买完域名还需要什么有免费的wordpress
  • 滨州企业网站建设360网页版登录入口
  • pedl中文模组网站html教程菜鸟教程视频
  • 网站后台更新没有变化中云建设集团网站
  • 网站提交收录软件综述题建设网站需要几个步骤
  • 深圳去聋哑做义工申请网站怎么做网页菜单
  • 北京seo优化wyhseo北京网站优化提供商
  • 绵阳网站维护托管微信小程序 模板
  • 什么叫宣传型网站wordpress 微信主题下载
  • 制作网站的模板下载软件国外主流网站开发技术
  • 全国企业信息官网网站网站模板 国外
  • 招聘网站开发的公司网站不用域名可以吗
  • seo培训学校排名青岛济南网站建设优化
  • phpcms v9 网站建设设计制作网络科技模板ps做网站显示内容参考
  • 推荐十个网站展馆设计论文