简体   繁体   中英

access unmanaged dll in c++ from two different applications (a c# windows service and a software platform)

I have spent several hours looking for the answer so I figured out that if I ask myself I could finally end up with a solution to my problem. First, to describe what I want to do: 打包的DLL和软件平台需要共享未经管理的代码 So, to describe the design above, I have a .dll plugin written in pure c++ ( I don't want it to contain any .Net code). This c++ plugin will be used from a software platform. Now, I want the c# windows service to interact with the software platform, but it only can do it through the c++ dll. For this reason, I wrapped all the functionality of the c++ plugin into the Wrapped Dll (I exported the functionality) and I am using the Wrapped Dll into the c# windows service.

Now, I have a test method to check if the export of the c++ functionality is working ok, (just a simple method that returns the sum of two integers) and I can see that the windows service can interract with the c++ unmanaged dll in the correct way, I am getting the correct results. The problem I have is when I try to interact with the Software platform through the c++ unmanaged dll. I get the following error:

错误的内存损坏

I realized that the error should be because the unmanaged dll cannot be accessed by the Software Platform and the Wrapped Dll at the same time. To solve this, I found out that the solution is to add the unamanaged dll in the GAC. But to put a dll in the GAC, the dll has to have a strong name, but I found out that I cannot sign a strong name to unmanaged code unless I have the /clr option enabled. But when I have this option enabled the unmanaged dll doesn't compile.

To summarize, is there any way I can get my unmanaged dll to be access by the software platform and from the wrapped dll? Without the integration of .Net in my unmanaged code?

I have spent a lot of hours searching for the solution so it would be great if any of you can show me a little light in the tunnel!

Thank you

Your unmanaged dll's and all it's dependecies must be in the same folder as your application (.exe). You don't need the GAC. If you want to store your dlls somewhere else, just use:

p(Invoke:

[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
static extern bool SetDllDirectory(string lpPathName);

[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
static extern bool AddDllDirectory(string lpPathName);

And on your application startup:

if (System.Environment.OSVersion.Version.Major > 5)
{
    AddDllDirectory(System.Windows.Forms.Application.StartupPath + "\\bin64");
}
else
{
    SetDllDirectory(System.Windows.Forms.Application.StartupPath + "\\bin64");
}

Also be sure you do not mix up x86 and x64 architexture. Furthermore be sure you are using the right CallingConvention. A good example is this: Unhandled Exception: System.AccessViolationException: Attempted to read or write

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM