[RESOLVED] [3.0/LINQ] DLLImport from a C++ DLL
Hello,
VS 3.5
I have created a simple C++ DLL, and I want to use this in my C# program.
However, I am getting a Can't find PInvoke DLL 'TEST_SIP.dll
I have placed the DLL in my debug folder where my C# program is executed from. I am not sure if that is the correct place to put it.
Not sure where I am going wrong with this one.
many thanks for any suggestions,
Steve
Code in my C#
Code:
[DllImport("TEST_SIP.dll")]
public static extern double Add(double a, double b);
private void btnLogin_Click_1(object sender, EventArgs e)
{
string msg = SendData("Hello how are you");//Can't find PInvoke DLL 'TEST_SIP.dll'.
MessageBox.Show(msg);
}
C++ header
Code:
//TEST_SIP.h
#include <iostream>
namespace TEST_SIP
{
class TEST_SIP_Phone
{
public:
static _declspec(dllexport) double Add(double a, double b);
static _declspec(dllexport) double Subtract(double a, double b);
static _declspec(dllexport) double Multiply(double a, double b);
static _declspec(dllexport) double Divide(double a, double b);
static _declspec(dllexport) std::string SendData(std::string data);
};
}
C++ Functions
Code:
//TEST_SIP.cpp
#include "TEST_SIP.h"
#include <stdexcept>
#include <cctype>
using namespace std;
namespace TEST_SIP
{
double TEST_SIP_Phone::Add(double a, double b)
{
return a + b;
}
double TEST_SIP_Phone::Subtract(double a, double b)
{
return a - b;
}
double TEST_SIP_Phone::Multiply(double a, double b)
{
return a * b;
}
double TEST_SIP_Phone::Divide(double a, double b)
{
if(b == 0)
{
throw new invalid_argument("b cannot be zero!");
}
return a / b;
}
std::string TEST_SIP_Phone::SendData(std::string data)
{
return data;
}
}
Re: [3.0/LINQ] DLLImport from a C++ DLL
Try registering the DLL via the command prompt. Or storing it in the System32 folder.
Re: [3.0/LINQ] DLLImport from a C++ DLL
Regardless of where it was stored, it would still have to be registered.
Re: [3.0/LINQ] DLLImport from a C++ DLL
To register the DLL simply type the following in the command prompt...
Regsvr32 "C:\test\test.dll"
Re: [3.0/LINQ] DLLImport from a C++ DLL
Isn't registration only for COM components? If this unmanaged library is not a COM component then it shouldn't need to, and in fact couldn't, be registered. Am I mistaken?
Re: [3.0/LINQ] DLLImport from a C++ DLL
Unmanaged non-COM libraries cannot be registered.
.NET assemblies can't be registered unless they expose a COM interface. If the assembly is not loaded into the GAC, you could use gacutil to manually import it.
Unmanaged DLLs are searched for in the directories specified in the PATH environment variable. If you have it in another, you must fully qualify the path in the DllImport attribute.
You should also mark your C++ functions as stdcall. I think the default is cdecl, even for exported functions.
Re: [3.0/LINQ] DLLImport from a C++ DLL
Problem solved, should have used the following:
extern "C" _declspec(dllexport) int AddNumber(int a, int b);
Steve