Export methods from a dll in C++

Asked

Viewed 253 times

1

I’m having a problem, my dll has not exported the methods for me to use in other programs.

I did it this way:

namespace integration {

    class RESTRequest {
        public:
            __declspec(dllexport) string GetPing(char* ping);
        private:
            static void MarshalString(String ^ s, string& os);
    };

}

It is compiling nice, but when I try to access the ddl with a java program, give this error:

Exception in thread "main" java.lang.UnsatisfiedLinkError: Error looking up function 'GetPing': The specified procedure could not be found.

For me the export took place with __declspec(dllexport) in the way, but by the way not only worked with it, I have to add some more deltalhe in dll ?

  • I think a name inside a namespace cannot be exported to Java...if it is a class method, then it must be a "static" method... extern "C"...in a simple case (without classes and without namespaces) would look like this: extern "C" declspec(dllexport) GetPing(char* ping);

  • In the commentary I put "declspec", it is "__declspec", of course

1 answer

0


Visual Studio 2005 Compiler (used in command line)

DLL

#include <iostream>
using namespace std;

namespace N
{
    class __declspec(dllexport) C
    {
      public:
         void display();
         static C* createNewC() { return new C; }
   };
}

void N::C::display()
{
   cout << "*\n";
   cout << "* hello from N::C::display\n";
   cout << "*\n";
}

TEST

namespace N
{
   class __declspec(dllimport) C
   {
      public:
         void display();
         static C* createNewC();
   };
}

int main()
{
   N::C* c = N::C::createNewC();
   c->display();
}

COMPILATION OF THE DLL

Wed  1 Feb 2017 04:52:00  >> { F:\projects\misc }                                  
$ cl /EHsc /LD testdll.cpp                                                         
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 14.00.50727.762 for 80x86   
Copyright (C) Microsoft Corporation.  All rights reserved.                         

testdll.cpp                                                                        
Microsoft (R) Incremental Linker Version 8.00.50727.762                            
Copyright (C) Microsoft Corporation.  All rights reserved.                         

/out:testdll.dll                                                                   
/dll                                                                               
/implib:testdll.lib                                                                
testdll.obj                                                                        
Creating library testdll.lib and object testdll.exp                             

COMPILATION OF THE TEST

$ cl /EHsc testdllmain.cpp testdll.lib                                                
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 14.00.50727.762 for 80x86      
Copyright (C) Microsoft Corporation.  All rights reserved.                            

testdllmain.cpp                                                                       
Microsoft (R) Incremental Linker Version 8.00.50727.762                               
Copyright (C) Microsoft Corporation.  All rights reserved.                            

/out:testdllmain.exe                                                                  
testdllmain.obj                                                                       
testdll.lib                                                                           

TEST RUN

Wed  1 Feb 2017 04:45:04  >> { F:\projects\misc }                                     
$ testdllmain.exe                                                                     
*                                                                                     
* hello from N::C::display                                                            
*                                                                                     
  • still lacked to export functions usable by Java, but now it becomes easier

Browser other questions tagged

You are not signed in. Login or sign up in order to post.