Win 7 DllImport C# Odd error, Invalid access to me

2019-04-06 09:08发布

I am using DllImport to access some functions in a C++ dll from my C# application.

This code works fine on my dev laptop, which is Windows 7 64bit, the dll itself is 32 bit, so I run the process hosting the dll in 32bit and it works well. However when I try to run the exact same process on my target machine, which is again, Windows 7 64bit Ultimate i get the error 'Invalid access to memory location.' from the process.

I'm not sure what the problem is, i've looked at loads of resources on the net and none of them have solved it for me. I dont understand why it works fine on my dev box, but not on the target?

The dll itself is fine, the examples that come with the dll all work fine on my target box (which are C# apps doing DllImport).

Has anyone else had this problem? Been fighting it for two days now!

Exception: {"Unable to load DLL 'CLEyeMulticam.dll': Invalid access to memory location. (Exception from HRESULT: 0x800703E6)"}

标签: c# dllimport
10条回答
祖国的老花朵
2楼-- · 2019-04-06 09:58

I have encountered a problem with a 64bit .NET app ("Any CPU") trying to load a 32bit native DLL dependency. I don't have the error message in front of me, so I can't tell you if it is the same problem. The solution for my problem was to change my build to x86-only.

If the bit size of the DLL is changing on each box, maybe there are structure size differences, so your PInvoke signature becomes incorrect. This could easily cause a buffer overrun, and cause stack corruption in the native code.

查看更多
在下西门庆
3楼-- · 2019-04-06 10:01

@Merlyn Morgan-Graham We faced a similar problem. Where we built the .Net application with "Any CPU" build and tried using with 32 bit C++ Dll. When we run the .Net Application in 64 bit OS. It runs as 64 bit executable and hence and got similar issue. After buidling in X86 Loading, Calls to C++ Dlls worked absolutely fine. One more important thing is if you are using C++ DLL in your .Net code. There would be fair amount of marshalling, so it is important to stick to the build type (i.e. X86, any CPU or X64).

Please check the following link also: Windows Vista: Unable to load DLL 'x.dll': Invalid access to memory location. (DllNotFoundException)

查看更多
手持菜刀,她持情操
4楼-- · 2019-04-06 10:12

I ran into a similar issue when I moved to win7. You might need to set your (64 bit) machine to run as a 32bit one by-default, using the below command:

C:\WINDOWS\Microsoft.NET\Framework64\v2.0.50727\Ldr64.exe setwow

Ref: http://social.msdn.microsoft.com/Forums/en/phoenix/thread/9a43e9a1-a744-4a1a-bb34-3604254c126b

查看更多
Lonely孤独者°
5楼-- · 2019-04-06 10:13

I notice one big difference between your dev machine and your target machine, the dev environment. Make sure you have all the necessary redistributables on the target machine.

Edit: I have seen similar issues when some dlls were compiled to different versions of the .Net framework or if they were made with different versions of Visual Studio, as the redistributables for each version are different and the latest redistributables are not exactly backwards compatible.

查看更多
登录 后发表回答