C# & C++ Load MfcDll

本文介绍了一个使用MFC实现的DLL,并展示了如何通过C#进行调用的过程。具体包括字符串处理、数值运算及字节数据操作等功能的实现与调用示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//MfcDll:

BSTR CMyMFCdllApp::MyDllTest(TCHAR* x)
{
    CString str(x);
    str+="_经过MFC处理的数据";
    return str.AllocSysString() ;
}

int CMyMFCdllApp::MyDllNumTest(int x)
{
 int y=20;
 return y*x;
}

byte* CMyMFCdllApp::MyDllByteTest(int ByteLenght)
{
   byte *bx=new byte[ByteLenght];
   for(int i=0;i<ByteLenght;i++)
   {
    bx[i]=212;
   }
   return bx;
}
//MyMFCdll.def

; MyMFCdll.def : 声明 DLL 的模块参数。

LIBRARY      "MyMFCdll"

EXPORTS
      MyDllStringTest
      MyDllNumTest
      MyDllByteTest
    ; 此处可以是显式导出

     DllCanUnloadNow     PRIVATE
     DllGetClassObject   PRIVATE
     DllRegisterServer   PRIVATE
     DllUnregisterServer PRIVATE

 

 

C#:

        using System.Runtime.InteropServices ;

        [DllImport("MyMFCdll.dll", EntryPoint = "MyDllStringTest", CharSet = CharSet.Unicode)]
        private static extern IntPtr  MyDllStringTest(string x);

        [DllImport("MyMFCdll.dll", CharSet = CharSet.Unicode)]
        private static extern int MyDllNumTest(int x);

        [DllImport("MFCdll.dll", EntryPoint = "MyDllByteTest", CharSet = CharSet.Unicode)]
        private static extern IntPtr MyDllByteTest(int ByteLenght);

        private void button4_Click(object sender, EventArgs e)
        {
//string:
            MessageBox.Show(Marshal.PtrToStringBSTR(MyDllStringTest("中文123abc")));
//Num:           
            MessageBox.Show(MyDllNumTest(100).ToString());
//Byte:
            int ByteLenght = 1000;
            byte[] bx = new byte[ByteLenght];
            IntPtr p= MyDllByteTest(ByteLenght);

            for (int i = 0; i < ByteLenght; i++)
            {
                bx[i] = Marshal.ReadByte(p, i);
            }
        }

 

C++ :

 typedef BSTR (WINAPI *MyFunC)(TCHAR*);
 HINSTANCE hDllInst = ::LoadLibrary(_T("MyMFCdll.DLL"));
 MyFunC DllFun = (MyFunC)::GetProcAddress(hDllInst,"MyDllTest");
 ::AfxMessageBox(DllFun(_T("MFCDLL动态调用")));
 ::FreeLibrary(hDllInst);

 

 

//c# Load MSDOS RegDll

            System.Diagnostics.Process p = new System.Diagnostics.Process();
            p.StartInfo.FileName = "cmd.exe";
            p.StartInfo.UseShellExecute = false;
            p.StartInfo.RedirectStandardInput = true;
            p.StartInfo.RedirectStandardOutput = true;
            p.StartInfo.RedirectStandardError = true;
            p.StartInfo.CreateNoWindow = true;
            p.Start();

            p.StandardInput.WriteLine(@"MD C:/123");
            p.StandardInput.WriteLine(@"subst U: C:/123");

            p.Close();

 

//c# Auto RegDll

        using System.Runtime.InteropServices ;

 

        [DllImport("MyMFCdll2.dll", EntryPoint = "DllUnregisterServer")]
        static extern int DllUnregisterServer();

 

        [DllImport("MyMFCdll2.dll", EntryPoint = "DllRegisterServer")]
        static extern int DllRegisterServer();

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值