问题
I run into an issue calling a c++ function from a c# program. It works fine until I add the line "pcl::io::savePCDFileASCII("test_pcd.pcd", cloud);
". Then I get the error:
Exception thrown: 'System.DllNotFoundException' in MIxed_Mode_Calling_App.exe
An unhandled exception of type 'System.DllNotFoundException' occurred in MIxed_Mode_Calling_App.exe
Unable to load DLL 'C:\Users\d\Box\Daniel\RE\Surface Project\PCL_Project\MixedMode\build\Debug\MixedMode.dll': The specified module could not be found. (Exception from HRESULT: 0x8007007E)
The location is correct and the MixedMode.dll can be found at that location.
Why is the above named line causing issues in the dll but works fine in a simple .cpp program?
Thanks!
c++ function "MixedMode.h":
#ifndef MIXED_MODE_MULTIPLY_HPP
#define MIXED_MODE_MULTIPLY_HPP
#include <iostream>
#include <pcl/io/pcd_io.h>
#include <pcl/point_types.h>
extern "C"
{
__declspec(dllexport) int __stdcall mixed_mode_multiply(int a, int b) {
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 (auto& point : cloud)
{
point.x = 1024 * rand() / (RAND_MAX + 1.0f);
point.y = 1024 * rand() / (RAND_MAX + 1.0f);
point.z = 1024 * rand() / (RAND_MAX + 1.0f);
}
pcl::io::savePCDFileASCII("test_pcd.pcd", cloud); //If this line is removed it works.
std::cerr << "Saved " << cloud.size() << " data points to test_pcd.pcd." << std::endl;
for (const auto& point : cloud)
std::cerr << " " << point.x << " " << point.y << " " << point.z << std::endl;
return (0);
}
}
#endif
C++ "MixedMode.cpp":
#include "MixedMode.h"
c# program "Program.cs":
using System;
using System.Runtime.InteropServices;
namespace Mixed_Mode_Calling_App
{
public class Program
{
[DllImport(@"C:\Users\d\Box\Daniel\RE\Surface Project\PCL_Project\MixedMode\build\Debug\MixedMode.dll", EntryPoint =
"mixed_mode_multiply", CallingConvention = CallingConvention.StdCall)]
public static extern int Multiply(int x, int y);
public static void Main(string[] args)
{
int result = Multiply(7, 7);
Console.WriteLine("The answer is {0}", result);
Console.ReadKey();
}
}
}
As part of the troubleshooting, I created this .cpp program which runs fine.
#include <iostream>
#include <pcl/io/pcd_io.h>
#include <pcl/point_types.h>
int
main()
{
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 (auto& point : cloud)
{
point.x = 1024 * rand() / (RAND_MAX + 1.0f);
point.y = 1024 * rand() / (RAND_MAX + 1.0f);
point.z = 1024 * rand() / (RAND_MAX + 1.0f);
}
pcl::io::savePCDFileASCII("test_pcd.pcd", cloud);
std::cerr << "Saved " << cloud.size() << " data points to test_pcd.pcd." << std::endl;
for (const auto& point : cloud)
std::cerr << " " << point.x << " " << point.y << " " << point.z << std::endl;
return (0);
}
来源:https://stackoverflow.com/questions/64466135/calling-a-c-function-from-c-sharp-program