我有一个ASP.net应用程序试图使用pInvoke从c ++ dll(称为Dll1.dll)调用方法。
这是Dll1.cpp:
Tester::Tester() {
}
void Tester::hello() {
int i = 0;
}
DLL_API void Tester_hello(Tester* pObj) { pObj->hello(); }
DLL_API Tester* Tester_Create() { return new Tester(); }
DLL_API void Tester_Delete(Tester* pObj) { delete pObj; }
这是DLL.h:
#define DLL_API __declspec(dllexport)
class DLL_API Tester {
public:
Tester();
void hello();
};
这就是我试图在我的asp.net应用程序中加载dll的地方。注意我从WebApiConfig调用它只是为了尝试这个并确保它有效:
public static class WebApiConfig
{
[DllImport("Dll1.dll")]
static extern IntPtr Tester_Create();
[DllImport("Dll1.dll")]
static extern void Tester_hello(IntPtr pObj);
[DllImport("Dll1.dll")]
static extern void Tester_Delete(IntPtr pObj);
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
// Configure Web API to use only bearer token authentication.
config.SuppressDefaultHostAuthentication();
config.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType));
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
IntPtr h = Tester_Create(); <- Exception thrown here
Tester_hello(h);
Tester_Delete(h);
}
}
但是,当我运行以下应用程序时,我收到此错误:
[DllNotFoundException:无法加载DLL&#39; Dll1.dll&#39;:此操作仅在应用容器的上下文中有效。 (HRESULT的例外情况:0x8007109A)]
这里出了什么问题?