Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

reference a C++ dll in a C# program

Status
Not open for further replies.

robert201

Programmer
Jul 18, 2007
80
TH
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;		
	}	
}
 
Hello,

I was thinking about that. However, the documents I read said nothing about adding a reference.

How would I add a reference?

Many thank for your help,

Steve
 
Go to Project --> Add Reference.

Then click the "Browse" tab and browse to your DLL's location, and add the reference.

Hope this helps,

Alex

[small]----signature below----[/small]
Majority rule don't work in mental institutions

My Crummy Web Page
 
Hello,

I went to browse for the dll, however when I click ok to add it I get the following message: "A reference to 'TEST_SIP' could not be added"

Thanks for anymore advice,

Steve
 
Hello,

I found the solution.

You have to create a SmartDevice WIN32 DLL. Add it into your project. Project | Existing Item | Set it property to "copy if newer".

The code is pretty much the same.

Steve
 
FWIW, you probably ought to mention you're using the compact framework sooner in the future...

Glad you got it working :)

Alex

[small]----signature below----[/small]
Majority rule don't work in mental institutions

My Crummy Web Page
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top