Unable to load DLL 'Dll1.dll': This operation is only valid in the context of an app container. (Exception from HRESULT: 0x8007109A)

2

I have an ASP.net application that is trying to invoke methods from a c++ dll (called Dll1.dll) using pInvoke.

This is the 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; }

This is the DLL.h:

#define DLL_API __declspec(dllexport)

class DLL_API Tester {
public:
    Tester();
    void hello();
};

And this is where I am trying to load the dll in my asp.net application. Note I am calling it from the WebApiConfig just to try this and make sure it works:

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);
    }
}

However, when I run the following application, I get this error:

[DllNotFoundException: Unable to load DLL 'Dll1.dll': This operation is only valid in the context of an app container. (Exception from HRESULT: 0x8007109A)]

What is going wrong here?

c#
c++
asp.net
.net
dll
asked on Stack Overflow Oct 26, 2016 by KPERI

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0