简体   繁体   中英

Using x64 c++ dll in Unity gives “Is not a valid Win32 application error”

What I want to do

I am trying to use some of the Point Cloud Library (PCL) functionalities in Unity 64bits. To do so, I proceed like explained in this tutorial which is about using the OpenCV library in Unity.

What I did and the issue I got

So I installed the PCL library (v1.8, x64) , I linked it to a Visual Studio C++ project and got a simple function using it ( the getting started example of the library ).

Then I compiled this project as a x64 release dll that I included in Unity in the Asset/Plugins folder, as well as the PCL release dlls.

Finally I use the following lines to load the dll in unity and make MySimpleFunction available to my C# script.

[DllImport("PCLToolsForUnity")] 
internal static extern int MySimpleFunction();

The issue is that when I run the C# script calling MySimpleFunction(), Unity shows the following error:

Failed to load 'Assets/Plugins/PCLToolsForUnity.dll' with error '%1 is not a valid Win32 application.

What I already checked and tried

I read that it could be caused by mixing 32 bits and 64 bits dll and application, however my Unity installation is 64 bits as well as the dll I included (I even checked with dumpbin /headers command). So I don't even know why Unity is expecting my dll to be a Win32 application..

I also checked that there is no space in the path to the dlls since I read it could be another cause for this error.

Beforehand I tried all this with a c++ project using no library and this worked fine. So it could be caused by the linked library

Any idea on what could cause this error and how to solve it?


Edit

I am working on a x64 machine.

After more tests, it seems that the issue is my DLL itself. When I run any function with the following command: C:\\Windows\\SysWOW64\\rundll32.exe PCLToolsForUnity.dll, testFunction , I get the same error as in Unity: PCLToolsForUnity.dll is not a valid Win32 application .

I also found the line that "breaks" my dll. One of the functions I export is the following:

PCLTOOLSFORUNITY_API int pcdWrite(void)
{
pcl::PointCloud<pcl::PointXYZ> cloud;

// Fill in the cloud data
cloud.width = 5;
cloud.height = 1;
cloud.is_dense = false;
cloud.points.resize(cloud.width * cloud.height);

for (size_t i = 0; i < cloud.points.size(); ++i)
{
    cloud.points[i].x = 1024 * rand() / (RAND_MAX + 1.0f);
    cloud.points[i].y = 1024 * rand() / (RAND_MAX + 1.0f);
    cloud.points[i].z = 1024 * rand() / (RAND_MAX + 1.0f);
}

pcl::io::savePCDFileASCII("test_pcd.pcd", cloud);
std::cerr << "Saved " << cloud.points.size() << " data points to test_pcd.pcd." << std::endl;

for (size_t i = 0; i < cloud.points.size(); ++i)
    std::cerr << "    " << cloud.points[i].x << " " << cloud.points[i].y << " " << cloud.points[i].z << std::endl;

return (0);

}

I noticed that if I comment the line pcl::io::savePCDFileASCII("test_pcd.pcd", cloud); and recompile my dll, then it works, I can run the dll's functions. When compiling the dll without this line, I can call the functions from it and it's working.

I checked the libraries I linked to the project and it seems fine to me, so still no idea where my problem is comming from. I hope those new information will help.

I finally found the cause for this thanks to Dependancy Walker .

It was actually an issue of bad linking. PCL uses the OpenNi library and my variable path linked to the 32 bit version of this library instead of the 64 bits.

For future users who see this issue, you can also spot which .dll's are x86 vs x64 with: https://github.com/lucasg/Dependencies

This tool is still being maintained.

In my case, this error came from my .dll grabbing a 32-bit Freetype dependency from elsewhere in the operating system. Simply dragging the appropriate Freetype.dll in with my custom .dll solved that issue.

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