Results 1 to 7 of 7

Thread: [RESOLVED] [3.0/LINQ] DLLImport from a C++ DLL

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Dec 2001
    Posts
    1,331

    Resolved [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;		
    	}	
    }
    steve

  2. #2
    Interweb adm/o/distrator Paul M's Avatar
    Join Date
    Nov 2006
    Location
    Australia, Melbourne
    Posts
    2,306

    Re: [3.0/LINQ] DLLImport from a C++ DLL

    Try registering the DLL via the command prompt. Or storing it in the System32 folder.

  3. #3
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: [3.0/LINQ] DLLImport from a C++ DLL

    Regardless of where it was stored, it would still have to be registered.

  4. #4
    Interweb adm/o/distrator Paul M's Avatar
    Join Date
    Nov 2006
    Location
    Australia, Melbourne
    Posts
    2,306

    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"

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    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.
    Last edited by penagate; Mar 8th, 2008 at 07:39 PM.

  7. #7

    Thread Starter
    Frenzied Member
    Join Date
    Dec 2001
    Posts
    1,331

    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
    steve

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width