如何修复“对内存位置的无效访问”错误?

时间:2022-02-26 22:30:08
    [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    public static extern IntPtr LoadLibrary(string lpFileName);

    [DllImport("kernel32.dll")]
    public static extern IntPtr GetProcAddress(IntPtr hModule, string procedureName);

    [DllImport("kernel32.dll")]
    public static extern bool FreeLibrary(IntPtr hModule);

    static void Main(string[] args)
    {
        IntPtr handle = LoadLibrary(@"ItwNidSmart.dll");
        if (handle == IntPtr.Zero)
        {
            try
            {
                int hr = Marshal.GetHRForLastWin32Error();
                Marshal.ThrowExceptionForHR(hr);
            }
            catch (Exception ex)
            {
                Console.Write("Error: "+ ex.Message);
            }
        }
        IntPtr proc = GetProcAddress(handle, "InitializeModule");
    }

I try to load this native C++ library in my Windows 7 x64, but I got this error. I've already built this solution to x86 application.

我试图在我的Windows 7 x64中加载这个本机c++库,但是我得到了这个错误。我已经为x86应用程序构建了这个解决方案。

The error occurs at the call to LoadLibrary().

错误发生在调用LoadLibrary()时。

1 个解决方案

#1


1  

You didn't actually state the exact error message, and which line it occurs on. Is it possible that you are erroneously linking to the 32 bit version of ItwNidSmart.dll?

实际上,您并没有声明确切的错误消息,以及发生的行。是否可能您正在错误地链接到32位版本的ItwNidSmart.dll?

In fact, your P/Invokes are wrong, which may or may not be the cause of your problem. The most important error is that GetProcAddress specifies the procedure name as an ANSI string. They should read:

事实上,您的P/调用是错误的,这可能是也可能不是问题的原因。最重要的错误是GetProcAddress将过程名指定为ANSI字符串。他们应该阅读:

[DllImport("kernel32", SetLastError=true)]
static extern IntPtr LoadLibrary(string lpFileName);

[DllImport("kernel32", CharSet=CharSet.Ansi, ExactSpelling=true, SetLastError=true)]
static extern UIntPtr GetProcAddress(IntPtr hModule, string procName);

[DllImport("kernel32.dll", SetLastError=true)]
static extern bool FreeLibrary(IntPtr hModule);

I'm not sure that these errors are actually causing you problems.

我不确定这些错误是否真的给你带来了问题。


EDIT

编辑

You state in a comment that the failure occurs at the call to LoadLibrary(). If this raises an exception then the only explanation that I can come up with is that the fault lies in the DLLMain() of the DLL and not in the C# code. If the DLL was the wrong bitness, or not found, then LoadLibrary() would return NULL.

您在注释中声明失败发生在调用LoadLibrary()时。如果这引发了异常,那么我能想到的唯一解释是,错误在于DLL的DLLMain(),而不是c#代码。如果DLL是错误的位,或者没有找到,那么LoadLibrary()将返回NULL。

I think to solve this you need to look to the DLL and not the C# code.

我想要解决这个问题,你需要查找DLL而不是c#代码。

#1


1  

You didn't actually state the exact error message, and which line it occurs on. Is it possible that you are erroneously linking to the 32 bit version of ItwNidSmart.dll?

实际上,您并没有声明确切的错误消息,以及发生的行。是否可能您正在错误地链接到32位版本的ItwNidSmart.dll?

In fact, your P/Invokes are wrong, which may or may not be the cause of your problem. The most important error is that GetProcAddress specifies the procedure name as an ANSI string. They should read:

事实上,您的P/调用是错误的,这可能是也可能不是问题的原因。最重要的错误是GetProcAddress将过程名指定为ANSI字符串。他们应该阅读:

[DllImport("kernel32", SetLastError=true)]
static extern IntPtr LoadLibrary(string lpFileName);

[DllImport("kernel32", CharSet=CharSet.Ansi, ExactSpelling=true, SetLastError=true)]
static extern UIntPtr GetProcAddress(IntPtr hModule, string procName);

[DllImport("kernel32.dll", SetLastError=true)]
static extern bool FreeLibrary(IntPtr hModule);

I'm not sure that these errors are actually causing you problems.

我不确定这些错误是否真的给你带来了问题。


EDIT

编辑

You state in a comment that the failure occurs at the call to LoadLibrary(). If this raises an exception then the only explanation that I can come up with is that the fault lies in the DLLMain() of the DLL and not in the C# code. If the DLL was the wrong bitness, or not found, then LoadLibrary() would return NULL.

您在注释中声明失败发生在调用LoadLibrary()时。如果这引发了异常,那么我能想到的唯一解释是,错误在于DLL的DLLMain(),而不是c#代码。如果DLL是错误的位,或者没有找到,那么LoadLibrary()将返回NULL。

I think to solve this you need to look to the DLL and not the C# code.

我想要解决这个问题,你需要查找DLL而不是c#代码。