zoukankan      html  css  js  c++  java
  • C#调用C++DLL

    extern "C" __declspec(dllexportint MultiplyByTen(int numberToMultiply);

    extrn "C" _declspec(dllexport) int GenReg(char* id1, char *id2,char* string);

    #include "DynamicDLLToCall.h"

    int MultiplyByTen(int numberToMultiply)
    {
            int returnValue = numberToMultiply * 10;
            return returnValue;
    }
     

    int GenReg(char* id1, char *id2,char* string)

    {

       return 0;

    }

    2.导入DLL声明     

        #region P/Invoke helpers

       [DllImport("kernel32.dll")]
        public static extern IntPtr LoadLibrary(string dllToLoad);

        [DllImport("kernel32.dll")]
        public static extern IntPtr GetProcAddress(IntPtr hModule, string procedureName);

       [DllImport("kernel32.dll")]
       public static extern bool FreeLibrary(IntPtr hModule);

      #endregion

      private delegate int MultiplyByTen(int numberToMultiply);

      
      private delegate int GenRegProc(byte[] id1, byte[] id2, byte[] string);

    3.调用DLL

        IntPtr pDll = LoadLibrary("YourDll.DLL");

       IntPtr pAddressOfFunctionToCall = GetProcAddress(pDll, "MultiplyByTen");

       MultiplyByTen multiplyByTen = (MultiplyByTen)Marshal.GetDelegateForFunctionPointer(
                                                                                            pAddressOfFunctionToCall,
                                                                                            typeof(MultiplyByTen));

       IntPtr pAddressOfFunctionToCall = GetProcAddress(pDll, "GenReg");

      GenRegProc GenRegMethod = (GenRegProc)Marshal.GetDelegateForFunctionPointer(pAddressOfFunctionToCall,typeof(GenRegProc));

       int regCode = GenRegMethod(Encoding.ASCII.GetBytes(device), Encoding.ASCII.GetBytes(productCode), Encoding.ASCII.GetBytes(CodeGenerator.GeneratorKey)).ToString();


        int theResult = multiplyByTen(10);    bool result = FreeLibrary(pDll);

    http://blog.csdn.net/procedurecode/article/details/2244419

  • 相关阅读:
    语音识别算法阅读之CTC
    语音识别模型阅读之CLDNN
    声纹识别算法阅读之self-attentive x-vector
    Git链接两个远程仓库
    tortoisegit提交不到远程库问题解决记录
    安装 Git 命令之后,本地的工作区中的文件没有小图标解决办法
    .NET CLS(Common Language System)简介
    .NET CTS(Common Type System)简介
    C# 中间语言
    .NET 程序执行流程
  • 原文地址:https://www.cnblogs.com/lvcha/p/3189985.html
Copyright © 2011-2022 走看看