Конвертировать C# в CLR C ++ - PullRequest
       37

Конвертировать C# в CLR C ++

0 голосов
/ 26 апреля 2020

У меня небольшой C# фон. Теперь я собираюсь изменить некоторый код для проекта, который я получил. По некоторым причинам проект C# необходимо преобразовать в CLR / C ++.

Может кто-нибудь помочь мне преобразовать C# в CLR / C ++?

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.IO;

namespace CSharpDemo
{
    public partial class Form1 : Form
    {       
        [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
        delegate int InitIDCard([MarshalAs(UnmanagedType.LPWStr)] String userID, int nType, [MarshalAs(UnmanagedType.LPWStr)] String lpDirectory);        
        [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
        delegate void GetVersionInfo([MarshalAs(UnmanagedType.LPWStr)] String ArrVersion, int nLength);       

        InitIDCard pInitIDCard;        
        GetVersionInfo pGetVersionInfo;        

        public static class MyDll
        {
            [DllImport("Kernel32.dll")]
            public static extern IntPtr LoadLibrary(string path);

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

            [DllImport("Kernel32.dll")]
            private static extern IntPtr GetProcAddress(IntPtr hModule, string procName);

            public static Delegate LoadFunction<T>(IntPtr hModule, string functionName)
            {
                IntPtr functionAddress = GetProcAddress(hModule, functionName);
                if (functionAddress.ToInt64() == 0)
                {
                    return null;
                }
                return Marshal.GetDelegateForFunctionPointer(functionAddress, typeof(T));
            }
        }

        public bool LoadDllIDCard()
        {
            string DllPath = "DLL path"

            IntPtr hModule = MyDll.LoadLibrary(@DllPath);
            if (hModule == null)
            {                
                return false;
            }

            pInitIDCard = (InitIDCard)MyDll.LoadFunction<InitIDCard>(hModule, "InitIDCard");

            pGetVersionInfo = (GetVersionInfo)MyDll.LoadFunction<GetVersionInfo>(hModule, "GetVersionInfo");            

            if (pInitIDCard == null || pGetVersionInfo == null)
            {
                MyDll.FreeLibrary(hModule);
                return false;
            }
            return true;
        }

Фактически , моя проблема в том, что эта функция не может управлять тем, как она будет в CLR / C ++

public static Delegate LoadFunction<T>(IntPtr hModule, string functionName)
 {
    IntPtr functionAddress = GetProcAddress(hModule, functionName);
    if (functionAddress.ToInt64() == 0)
       {
          return null;
       }                       
    return Marshal.GetDelegateForFunctionPointer(functionAddress, typeof(T));
 }           

Обновление

В соответствии с предложением György Kőszeg я преобразовал как что:

public: static Delegate^ LoadFunction(IntPtr hModule, String^ functionName, Type^ type)
        {
            IntPtr functionAddress = GetProcAddress(hModule, functionName);
            if (functionAddress.ToInt64() == 0)
            {
                return nullptr;
            }           
            return Marshal::GetDelegateForFunctionPointer(functionAddress, type);
        }

Звоните:

pInitIDCard = (InitIDCard^)MyDll::LoadFunction(hModule, "InitIDCard", InitIDCard::typeid);

Проблема решена, спасибо!

...