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:48

I've had this issue before. I think your problem is with VS trying to open the file but not having permissions to read it. You need to make sure the account you're using has access to the DLL. Try disabling UAC to see if it works, or use an Administrator account. Or try giving Full Control on the DLL to Everyone.

EDIT: Could you run VS as administrator (right click -> Run As Administrator)? Could you put the DLL onto your desktop to try? Is there a folder structure difference between your working computer and the one that's failing? Also, can the DLL run fine if you execute it outside of VS (try running it as admin as well)?

HTH

查看更多
你好瞎i
3楼-- · 2019-04-06 09:49

The obvious, but probably lame solution would be to build C# side explicitly for 32-bit. Check how do you create out of proc host - programatically or by populating registry keys or ... could be that on other box it gets set up to either make 64-bit hosting process or attempt in-proc invocation, which means loading ... It it's a registry setting don't forget that for mixed 23/64 bit cases there are two branches to dig into.

查看更多
对你真心纯属浪费
4楼-- · 2019-04-06 09:51

The DLL loading may crash because of unresolved dependencies, so open your DLL on target machine using Dependency Walker and see is there any problems.

查看更多
Bombasti
5楼-- · 2019-04-06 09:53

I had the same problem here Native loading works good. Loading from .net gives error Unable to load DLL 'my.dll': Invalid access to memory location

The problem was in DEP feature. When I switched on DEP for essential programs only, it gave no effect. But when I completely switched off DEP, and rebooted my server, error has gone. The one more thing I've done - installed latest updates for .net 4.0

The one notable thing - i didn't see any errors about DEP, just closing with "memory" error.

查看更多
唯我独甜
6楼-- · 2019-04-06 09:53

I have had similar problems before, try the following.

  • Check the .NET CLR version. Are there any SPs/KBs present in your target that is not in your dev?
  • Try loading the debug version of the C++ DLL. Are you able to load it? If it fails, I'd suggest starting your app under WinDBG in your target. Once the exception is hit, a simply !analyze -v will give you lots of info.
  • As a next step, I'd try to reproduce this issue in a unit test environment. Are the C# samples you mentioned built for x64 VM? If not, try doing that and try running the resulting sample binary in your target. Is the issue reproducible?
查看更多
干净又极端
7楼-- · 2019-04-06 09:56

If you're getting the error in your C# app, this message often indicates the native code did something nasty to memory that the ILM can see - check the code in/called by your DllMain routine - that is called before your call actually goes thru - if it's misbehaving, you'd see this result

查看更多
登录 后发表回答