首页 > 解决方案 > 32 位 WPF .NET 应用程序找不到其 32 位 dll

问题描述

我使用 P/Invoke,而不是 C++/CLI,并且正在使用 Visual Studio 2017。
我的应用程序是 WPF.NET——32 位,用 C# 编写。当我在我的 PCx64 上运行该应用程序时,它成功加载了它的 32 位 DLL,但是当我在虚拟机(64 或 32)上运行它时它崩溃了。我认为无法添加 DLL ...并且不知道为什么。
我的 DLL 是用 C++ 编写的,包括一个类,其中一些函数是用 Assembler 编写的。问题可能来自于它吗?

我的类.cpp:

int MyClass::Foo_1()  
{  
    __asm  
  {  
    mov eax, 0  
  }
}  
void MyClass::SetFoo_1()  
{  
  resultEAX = new int;  
  int a = -1;  
  try  
  {  
    a = Foo_1();  
  }  
  catch (int e) { }  
  if (a == 0) {  
  *resultEAX = 0;  
  }  
  else {  
    *resultEAX = 1;  
  }  
}  

MyClass.h:

#ifndef MYCLASS_H  
#define MYCLASS_H  
class __declspec(dllexport) MyClass {  
public:  
    int * resultEAX ;  
    int Foo1();  
    void SetFoo_1();  
    int  GetEAX();  
};  
#endif  

MyClassCaller.h:

extern "C" {  
#endif  
__declspec(dllexport) MyClass* Create();  
__declspec(dllexport) void Dispose(MyClass* a_pObject);  
__declspec(dllexport) void SetFoo_1(MyClass* a_pObject);  
__eclspec(dllexport) int GetEAX(MyClass* a_pObject);  
#ifdef __cplusplus
}  
#endif  

MyClassCaller.cpp:

Graphics* Create()  
{  
  return new MyClass();  
}  

void Dispose(MyClass * a_pObject)  
{  
  if (a_pObject != NULL)  
  {  
    delete a_pObject;  
    a_pObject = NULL;  
  }  
}  

void SetFoo_1(MyClass * a_pObject)  
{  
  if (a_pObject != nullptr)  
  {  
    a_pObject->SetFoo_1();  
  }  
}  

int GetEAX(MyClass * a_pObject)  
{  
  if (a_pObject != NULL)  
  {  
     return a_pObject->GetEAX();  
  }  
  return 0;  
}  

我使用托管 C# 代码从 WPF.NET 调用该类:

IntPtr pMyClass = MyClassHandling.Create();  
Int32 a = 0;  
Int64 b = 0;  
long c = 0;  
long rslt = 0;  

try  
{  
  MyClassHandling.SetFoo_1(pMyClass);  
  if (Environment.Is64BitOperatingSystem)  
  {  
    //"SysWOW64"  
    b = MyClassHandling.GetEAX(pMyClass);  
   //……  
  }  
  else  
  {  
    //"system32"  
    a = pMyClassHandling.GetEAX(pGraphics);  
    //…  
  }  

MyClassHandling.Dispose (pGraphics); 
pMyClass = IntPtr.Zero;  

[DllImport("SomeAssemblerFunctions.dll")]  
static public extern IntPtr Create();  
[DllImport("SomeAssemblerFunctions.dll")]  
static public extern void Dispose(IntPtr pGraphicsObject);  
[DllImport("SomeAssemblerFunctions.dll", EntryPoint = "SetGraphicMode1")]  
static public extern void SetFoo_1(IntPtr pGraphicsObject);  
[DllImport("SomeAssemblerFunctions.dll", EntryPoint = "GetEAX")]  
static public extern int GetEAX(IntPtr pGraphicsObject);

标签: c++wpfassemblyx86masm

解决方案


推荐阅读