Good day!

It is required to make ActiveX on c # c using C ++ library. How to do on with # ActiveX on the net many examples of good and different. But I did not find how to make a DLL for use with c # examples.

Using approach # 2, http://www.windows-tech.info/17/b11ab287a75874da.php approach # 2, created the project cppAx Visual C ++ | Win32 | Win32, put check marks DLL and Export symbols. Added a static library header to the project. The following file has been generated.

// cppAx.cpp : Defines the exported functions for the DLL application. // #include "stdafx.h" #include "cppAx.h" #include "MyStatLib.h" // This is an example of an exported variable CPPAX_API int ncppAx=0; // This is an example of an exported function. CPPAX_API int fncppAx(void) { return 42; } // This is the constructor of a class that has been exported. // see cppAx.h for the class definition CcppAx::CcppAx() { return; } 

Further it seems to be necessary to declare exported functions from a static library. But I do not know. Even if in this form I compile and try in another project to reference the resulting DLL, I get

A reference to XXX.dLL could not be added. Please make sure you have a valid assembly or COM component.

Thanks in advance for your help!

    2 answers 2

    In a C # project, you can add a link to another library only if it is a .NET assembly or a real COM component. Judging by your description, this is not so: in this case there is a regular DLL.

    It is necessary to make those characters from C ++ that are required in C # exported directly or through wrappers.

     #pragma comment(linker, "/EXPORT:somefunc=somefunc") CPPAX_API void somefunc() { // Вызвать функцию из статической библиотеки } 

    And then call them from C # using P / Invoke

     [DllImport("somelib.dll")] static extern somefunc(); 
    • I would like to clarify: - how to make a library suitable for reference? - CPPAX_API int fncppAx (void) {return 42} - like as an exported function? There may be stupid questions, but I practically do not know with ++. - gem
    • For a library to be suitable for reference, it must contain COM, which will be correctly registered in the system during installation. fncppAx - yes, this is an exported function, however, you need to check that the def-file contains the corresponding definition. - Nicolas Chabanovsky
    • In the help for def-files found: The keyword __ declspec (dllexport) adds export directives to the object file, therefore, there is no need to use a DEF file. It was not originally in my project. Is it possible in more detail about the "correct way of registration in the system during installation"? Am I doing something wrong, or do I need any additional movements? I am now just about the project created from scratch with the 1st machine generated function fncppAx. - gem
    • If you make a COM component in a DLL, it should be created using a different template, such as MFC ActiveX or something like that. Plus, before use, it must be registered with regsvr32.exe. But all this is necessary, only if the library remembers the state, if it is just a set of functions, then it is much easier to call them through DllImport. You can read it here: [Calling Win32 DLLs in C # with P / Invoke] [1], [Calling unmanaged functions from managed code] [1]. [1]: msdn.microsoft.com/en-us/magazine/cc164123.aspx [2]: msdn.microsoft.com/ru-ru/library/ms235282.aspx - Nicolas Chabanovsky
    • 1. C calls Win32 through P / Invoke does not cause any problems. 2. If you look at the code from the answer below, then there is also no way to make a reference to the DLL in C # (A reference to XXX.dLL could not be added ....) but nevertheless it does not interfere with P / Invoke to access functions . 3. In my project when I call a function, I get the error: Unable to find an entry point named 'fncppAx' in DLL 'cppAx.dll' [DllImport ("cppAx.dll")] protected static extern int fncppAx (); private void button1_Click (object sender, EventArgs e) {int i = fncppAx (); } - gem

    We use Unmanaged C ++ code in .NET programs .

    • I downloaded the project and when compiling (only lib) I got - LNK2001: unresolved external symbol Create C: \ ... \ Config.def and LNK1120: 1 unresolved externals. I have VS2010. Does it work for you? - gem
    • The compiler says that it cannot understand the name Create, because it sees several functions with that name. MSDN recommends setting the decorated function name, and recommends watching this most decorated name using DUMPBIN. We start Visual Studio Command Prompt (2010), do cd in Debug, do "dumpbin / symbols main.obj /out:db.txt". We look created db.txt, we look for that Create which is ours there. We take his decorated name (I have "? Create @@ YGPAXW4EObjectType @@@ Z"), check in the same command line that everything is correct "undname name", change in the Config.def project so that there was Create = them - Maxim Kamalov
    • I read rsdn.ru/forum/cpp/3874211.flat.aspx and then removed def from the project and removed the link in the project properties (dance with a tambourine). It all worked. But then I can’t use this "interesting and very elegant solution" - I don’t have a task to delve deeply into C ++, without which I don’t understand the code. I would only "P / Invoke - very simple, but not flexible." - gem