none
Include C++ library inside C# .exe file RRS feed

  • Question

  • Hello I'm currently using Costura Fody and its including inside all C# librarys, but how  to include inside C# application a C++(latest) library what do I need to use? Also if possible how to include C# .exe application inside C# .exe application(.Net 4.6) Thank you.

    P.S. for non C# .dll library I found this: Link 1 works Well, now is it possible with .exe applications?


    Monday, October 15, 2018 11:29 AM

All replies

  • Hi speed258,

    According to your description, you want to include C++ library inside C# .exe file, but this forum is discussing question about developing wpf, so I will move your thread to C# forum for professional help.

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, October 16, 2018 1:36 AM
  • Hi speed258,

    Is C++ library a manged library or a unmanged library?

    If it is a manged library, the same method that the link providing.

    If it is unmanaged library, which could not be merged into managed application, which need to Implicit PInvoke, for more information, please refer to:

    https://msdn.microsoft.com/en-us/library/2x8kf7zx.aspx

    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Tuesday, October 16, 2018 3:32 AM
    Moderator
  • Its unmanaged code, since if I want to add via reference in c# project it throws error that is failed to add.
    Tuesday, October 16, 2018 11:55 AM
  • Hi,

    try it using a vc++ wrapper class for your unmamaged code and use clr. After that you can just add the vc++ project as reference. Then you can just use the wrapper class to execute the code.

    Greetings, Chris

    PS: Can you please serve us some code? 
    • Edited by DerChris88 Tuesday, October 16, 2018 12:34 PM
    Tuesday, October 16, 2018 12:33 PM
  • What do mean by that:

    "how to include C# .exe application inside C# .exe application"

    Do you mean you want to start exe b somewhere isn the code of exe a and exe b is included in exe a?

    Is it possible to create a file after executing exe a? Or do you want to do it by using a stream? Is it possible to create dlls instead if exe?

    Tuesday, October 16, 2018 9:33 PM
  • Hi,

    try it using a vc++ wrapper class for your unmamaged code and use clr. After that you can just add the vc++ project as reference. Then you can just use the wrapper class to execute the code.

    Greetings, Chris

    PS: Can you please serve us some code? 

    Thisi is how my .dll .h file look like:

    #pragma once
    #ifdef PP4M1_EXPORTS
    #define PP4M1_API __declspec(dllexport)
    #else
    #define PP4M1_API __declspec(dllimport)
    #endif
    
    #ifdef __cplusplus
    extern "C" {
    #endif
    
    	PP4M1_API int ManageN(int request, int handle, int param);
    
    #ifdef __cplusplus
    }
    #endif

    Thursday, October 18, 2018 8:25 AM
  • What do mean by that:

    "how to include C# .exe application inside C# .exe application"

    Do you mean you want to start exe b somewhere isn the code of exe a and exe b is included in exe a?

    Is it possible to create a file after executing exe a? Or do you want to do it by using a stream? Is it possible to create dlls instead if exe?

    I want that in output folder will be only one "parent" .exe, but inside it have c++ .dll and .exe, that means these files wont be available from outside only "parent" .exe has access to these files of course I probably talking science fiction and in reality such action is not possible at all
    Thursday, October 18, 2018 8:43 AM
  • hmm ok, you want to do some static linking? For com dlls you could use ILMerge:

    https://www.microsoft.com/en-us/download/details.aspx?id=17630

    But the dll from c++ is not a com dll. So this is not working in your case.

    Thursday, October 18, 2018 10:58 AM
  • I would change the extern c dll by wrapping your code in a vc++ class, compiling it with clr, merge all dlls with ILMerge, and after that it should only be one exe file.
    Thursday, October 18, 2018 11:06 AM
  • I would change the extern c dll by wrapping your code in a vc++ class, compiling it with clr, merge all dlls with ILMerge, and after that it should only be one exe file.

    Ok after removing extern and creating class and turning on /clr everything went ok, I can freely use c++ class in c#, but after executing  methods from c++ which are .dll it throws: 

    is attempting managed execution inside OS Loader lock. Do not attempt to run managed code inside a DllMain or image initialization function since doing so can cause the application to hang.
    

    Turning off from exception settings does not solve the issue app crash.

    I followed  by this tutorial:

    https://www.youtube.com/watch?v=ctTFNNOzzWw

    Friday, October 19, 2018 7:41 AM
  • Hi speed258,

    can you show us the c++ code of your clr ref class from your dll? Have you got a DllMain in your dll? If yes, what do you want to do in the dll entry point and can you show it, too?

    Greeting, Chris


    • Edited by DerChris88 Friday, October 19, 2018 10:24 PM
    Friday, October 19, 2018 10:22 PM
  • Sure, sorry for late response.

    //includes, constants, typedef
    
    #pragma once
    
    using namespace System;
    
    namespace Preloader {
    
    	public ref class PreloaderV2
    	{
               private: bool CheckFiles()
               {}
               public: int ManageA(int argc, int adapter, int param)
    {
    return 1;
    }
    
    }
    }

    No I dont have DllMain I have two versions of same dll one created as non clr and second one as clr, first one got dllmain and second one doesnt. If I try to copy paste dllmain from first one to second one It shows DllMain as undefined.

    Sunday, October 21, 2018 10:23 AM
  • Hi,

    is it not possible to make a mixed clr dll?

    Greetings, Chris

    Sunday, October 21, 2018 5:29 PM
  • Hi,

    is it not possible to make a mixed clr dll?

    Greetings, Chris

    I dont want to mix my same dll's I used them for testing purpose, first one which is not CLR used before, now after advises I decided to create second one as CLR, but I get this error:

    "app.exe is attempting managed execution inside OS Loader lock. Do not attempt to run managed code inside a DllMain or image initialization function since doing so can cause the application to hang."

    Turning off from exception settings does not solve the issue app crash.

    I followed  by this tutorial:

    https://www.youtube.com/watch?v=ctTFNNOzzWw

    Also tried to create as static class, but its same error

    I have two ideas:

    1. Could It be that my whole methods with class is in .h file instead .cpp

    2. Could it be that is set wrong project settings which might cause the issue?

    Monday, October 22, 2018 7:01 AM

  • Do not attempt to run managed code inside a DllMain or image initialization function since doing so can cause the application to hang...


    This is telling you what not to do in the dllmain. Please show your dllmain...

    Ok, but you said "I want that in output folder will be only one "parent" .exe, but inside it have c++ .dll and .exe..." why you want to static link your tests in your programm? 

    Greetings, Chris

    Monday, October 22, 2018 7:50 AM

  • Do not attempt to run managed code inside a DllMain or image initialization function since doing so can cause the application to hang...


    This is telling you what not to do in the dllmain. Please show your dllmain...

    Ok, but you said "I want that in output folder will be only one "parent" .exe, but inside it have c++ .dll and .exe..." why you want to static link your tests in your programm? 

    Greetings, Chris

    From first dll dllmain look like this:

    // dllmain.cpp : Defines the entry point for the DLL application.
    #include "stdafx.h"
    
    BOOL APIENTRY DllMain(HMODULE hModule,
    	DWORD  ul_reason_for_call,
    	LPVOID lpReserved
    )
    {
    	switch (ul_reason_for_call)
    	{
    		case DLL_PROCESS_ATTACH:
    		case DLL_THREAD_ATTACH:
    		case DLL_THREAD_DETACH:
    		case DLL_PROCESS_DETACH:
    			break;
    	}
    	return TRUE;
    }

    Second one does not have.

    I'm talking instead: public: void MyMethod(){a+1;} I  tried to public: static void MyMethod(){a+1;}

    But same results

    Monday, October 22, 2018 11:06 AM
  • Hi,

    is managed debugging assistant activated? You may can avoid this issue by deactivating it. In the registry you can do it by setting HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\.NETFramework "MDA" to "0".

    Monday, October 22, 2018 7:17 PM
  • Hi,

    is managed debugging assistant activated? You may can avoid this issue by deactivating it. In the registry you can do it by setting HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\.NETFramework "MDA" to "0".

    Will this setting also will be needed to deactivate for other app users?
    Tuesday, October 23, 2018 11:00 AM
  • Or try to deactivate it using this instruction:

    https://docs.microsoft.com/en-US/dotnet/framework/debug-trace-profile/diagnosing-errors-with-managed-debugging-assistants

    You need to deactivate "LoaderLock"

    Greetings, Chris

    Tuesday, October 23, 2018 2:59 PM