Using mixed DLLs from / clr: clean projects

I am building a project along with a Dll.

The dll must support native code, so I declared it as / clr. My project was the starter project and the / clr project and everything was fine. However, I would like to enable some NUnit testing, so I had to switch my main project from / clr to / clr: pure.

Still compiles, but any Dll call generates a runtime error. When I go back to / clr everything is fine

In my Dll, the exported functions are declared like this:

#define DllExport   __declspec( dllexport )
DllExport bool DisplayScan(bool bShow, bool bAllPasses) { }

      

I also created a .def file containing the real names of all exported functions

LIBRARY "Controller"
EXPORTS
DisplayScan

      

From my main project, my import is declared like this:

#define _DllImport [DllImport("Controller.dll", CallingConvention = CallingConvention::Cdecl)] static
_DllImport bool DisplayScan(bool bShow, bool bAllPasses)

      

Has anyone faced such a problem?

+1


source to share


4 answers


Everything works well now

In fact, it works from the start.



Moral: don't try to use char * in std :: string

Strange thing: its ok in / clr until you return from the function. It will crash immediately in / clr: pure

+3


source


Basically, you are doing what is not supported; / clr: clean and native DLL export. As this MSDN article is quoted as saying , "Pure assemblies cannot export functions that are called from native functions, because entry points in a pure assembly use the __clrcall calling convention."



I'm not sure about the best workaround. However, with a little experimentation, you can probably use the __clrcall calling convention with the / clr option. Here's a link that might be helpful. From what I can put together, you should be able to export these managed classes and consume them from a managed assembly like your NUnit managed project, but keep your unmanaged export there with different method signatures. Keep in mind that once you expose any .net class via export, it will need to use the __clrcall calling convention.

+3


source


Benefits / clr: pure

Better performance: Since pure assemblies only contain MSIL, there are no native functionality and therefore no managed / unmanaged transitions required. (Function calls made through P / Invoke are an exception to this rule.)

AppDomain Awareness: CLR managed functions and data types exist within application scopes, which affects their visibility and availability. Pure assemblies are supported in the domain (each type assumes __declspec (appdomain)), so accessing their types and functions from other .NET components is easier and safer. As a result, pure assemblies interact more easily with other .NET components than mixed assemblies.

Boot without disk: Clean assemblies can be loaded into memory and even streamed. This is important for using .NET Collections as stored procedures. This is in contrast to mixed assemblies, which, due to their dependency on Windows boot mechanisms, must exist on disk in order to run.

Reflection. It is not possible to mirror mixed executables, whereas pure assemblies provide full support for reflection. See Reflection (C ++ / CLI) for more information.

Host manageability: Because pure assemblies contain only MSIL, they behave more predictably and flexibly than mixed assemblies when used in applications that host the CLR and change the default behavior.

/ Clr: pure restrictions

This section covers features that are currently not supported by / clr: pure.

Pure assemblies cannot be called by unmanaged functions. Therefore, pure assemblies cannot implement COM interfaces or call their own callbacks. Pure assemblies cannot export functions via __declspec (dllexport) or .DEF files. In addition, functions declared using the __clrcall convention cannot be imported via __declspec (dllimport). Functions in a native module can be called from a clean assembly, but pure assemblies cannot expose functions that are called by a callee, so the deployment of functionality in a pure assembly must be done using managed functions in a mixed assembly. See How-to Guide. For more information, go to / clr: pure (C ++ / CLI).

The ATL and MFC libraries are not supported by pure mode compilation in Visual C ++.

Pure .net modules are not accepted as input to the Visual C ++ linker. However, pure .obj files are accepted by the linker, and .obj files contain a superset of the information contained in netmodules. See .netmodule files as linker attachment for details.

COM compiler support (#import) is not supported, as this would result in unmanaged instructions in a clean assembly.

Floating point options for alignment and exception handling are not configurable for clean assemblies. As a result, __declspec (align) cannot be used. This displays some header files like fpieee.h incompatible with / clr: pure.

The GetLastError function in the PSDK can give undefined behavior when compiled with / clr: pure.

+1


source


your problem calls the convention CallingConvention = CallingConvention :: Cdecl ... define your function like this or use stdcall or clrcall, clecl for pure C

or is the problem here: determine that the extern function is not static

0


source







All Articles