【问题标题】:"DLLNotFoundException"?“DLLNotFoundException”?
【发布时间】:2013-03-05 03:44:17
【问题描述】:

我正在尝试将用 C++ 制作的本机 DLL 导入到 C#。我有一个小问题。

这是我的 C# 代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace test
{
    class Program
    {
        [DllImport("hello2dll.dll")] //I didn't know what to name it :'(
        private static extern void SayHi();

        static void Main(string[] args)
        {
            while (true)
            {
                Console.ReadKey();
                SayHi();
            }
        }
    }
}

这是来自 DLL 的 main.h:

#ifndef __MAIN_H__
#define __MAIN_H__

#include <windows.h>

/*  To use this exported function of dll, include this header
 *  in your project.
 */

#ifdef BUILD_DLL
    #define DLL_EXPORT __declspec(dllexport)
#else
    #define DLL_EXPORT __declspec(dllimport)
#endif


#ifdef __cplusplus
extern "C"
{
    #endif

     void DLL_EXPORT SayHi();

    #ifdef __cplusplus
}
#endif

#endif // __MAIN_H__

然后这里是来自 DLL 的 main.cpp:

#include "main.h"

#include<windows.h>

void SayHi()
{
    MessageBox(HWND_DESKTOP, "Hello!", "Hello!", 0);
}

所以我尝试通过将 DLL 放入 system32 来访问它,然后我尝试通过将其复制并粘贴到 Visual c# 中来将其添加到项目中,但到目前为止我还没有成功。我有点失望,它没有用,但谁知道呢。

【问题讨论】:

  • 你得到什么错误?或者你只是没有消息框?
  • 1. 32 位还是 64 位窗口?很多问题都是因为缺少适合平台的.dll造成的。

标签: c# c++ native native-code


【解决方案1】:

我不知道为什么它会失败,而是我会创建一个 CLR 兼容程序集

您可以使用tblimp 创建一个与 CLR 兼容的程序集,然后您可以添加对该程序集的引用而不是导入它。

【讨论】:

    猜你喜欢
    • 2011-05-26
    • 2012-11-01
    • 2013-07-26
    • 2011-03-17
    • 2012-11-05
    • 1970-01-01
    • 2015-07-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多