Porting DLL import from c# to c++Macro for dllexport/dllimport switchWhat is the difference between String and string in C#?Cast int to enum in C#What are the differences between a pointer variable and a reference variable in C++?How do I enumerate an enum in C#?What are the correct version numbers for C#?The Definitive C++ Book Guide and ListGet int value from enum in C#Metadata file '.dll' could not be foundWhat is the “-->” operator in C++?Why is reading lines from stdin much slower in C++ than Python?

Using font to highlight a god's speech in dialogue

Polarity of gas discharge tubes?

Received email from ISP saying one of my devices has malware

What are the French equivalents of "blow away the cobwebs"?

How would a disabled person earn their living in a medieval-type town?

What are ways to record who took the pictures if a camera is used by multiple people?

Is there anything in the universe that cannot be compressed?

How can an F-22 Raptor reach supersonic speeds without having supersonic inlets?

What is causing gaps in logs?

Can UV radiation be safe for the skin?

Why do modes sound so different, although they are basically the same as a mode of another scale?

Divide Numbers by 0

Does the telecom provider need physical access to the SIM card to clone it?

How are the cards determined in an incomplete deck of many things?

Can my UK debt be collected because I have to return to US?

To minimize the Hausdorff distance between convex polygonal regions

What is the definition of Product

Why wasn't Linda Hamilton in T3?

Can a human variant take proficiency in initiative?

How could reincarnation magic be limited to prevent overuse?

How does the search space affect the speed of an ILP solver?

Calculate Landau's function

Turn off Google Chrome's Notification for "Flash Player will no longer be supported after December 2020."

Is Chuck the Evil Sandwich Making Guy's head actually a sandwich?



Porting DLL import from c# to c++


Macro for dllexport/dllimport switchWhat is the difference between String and string in C#?Cast int to enum in C#What are the differences between a pointer variable and a reference variable in C++?How do I enumerate an enum in C#?What are the correct version numbers for C#?The Definitive C++ Book Guide and ListGet int value from enum in C#Metadata file '.dll' could not be foundWhat is the “-->” operator in C++?Why is reading lines from stdin much slower in C++ than Python?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








1















This is how I use/import the DLL using c#. How do I do it on c++ project?



[DllImport(@".x64something.dll", EntryPoint = "somthng", CharSet = CharSet.Unicode)]
static extern int somthng(string input);









share|improve this question





















  • 1





    Are you targeting the Windows platform?

    – J.R.
    Mar 28 at 4:56











  • What are you trying to do? Targeting a C# dll in C++? Or just including a C++ library in C++?

    – FranzHuber23
    Mar 28 at 8:17






  • 1





    In native c++ you probably want to have a macro change from __declspec(dllexport) when building the dll to __declspec(dllimport) when using the dll. Here is a question that should help: stackoverflow.com/questions/14980649/…

    – drescherjm
    Mar 28 at 13:18












  • Yes @J.R.......

    – TerribleDog
    Mar 29 at 0:09






  • 1





    @FranzHuber23 Use an external DLL in c++ that was also used in c# project

    – TerribleDog
    Mar 29 at 0:09

















1















This is how I use/import the DLL using c#. How do I do it on c++ project?



[DllImport(@".x64something.dll", EntryPoint = "somthng", CharSet = CharSet.Unicode)]
static extern int somthng(string input);









share|improve this question





















  • 1





    Are you targeting the Windows platform?

    – J.R.
    Mar 28 at 4:56











  • What are you trying to do? Targeting a C# dll in C++? Or just including a C++ library in C++?

    – FranzHuber23
    Mar 28 at 8:17






  • 1





    In native c++ you probably want to have a macro change from __declspec(dllexport) when building the dll to __declspec(dllimport) when using the dll. Here is a question that should help: stackoverflow.com/questions/14980649/…

    – drescherjm
    Mar 28 at 13:18












  • Yes @J.R.......

    – TerribleDog
    Mar 29 at 0:09






  • 1





    @FranzHuber23 Use an external DLL in c++ that was also used in c# project

    – TerribleDog
    Mar 29 at 0:09













1












1








1


1






This is how I use/import the DLL using c#. How do I do it on c++ project?



[DllImport(@".x64something.dll", EntryPoint = "somthng", CharSet = CharSet.Unicode)]
static extern int somthng(string input);









share|improve this question
















This is how I use/import the DLL using c#. How do I do it on c++ project?



[DllImport(@".x64something.dll", EntryPoint = "somthng", CharSet = CharSet.Unicode)]
static extern int somthng(string input);






c# c++ dll






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 28 at 2:48







TerribleDog

















asked Mar 28 at 0:31









TerribleDogTerribleDog

5711 silver badge20 bronze badges




5711 silver badge20 bronze badges










  • 1





    Are you targeting the Windows platform?

    – J.R.
    Mar 28 at 4:56











  • What are you trying to do? Targeting a C# dll in C++? Or just including a C++ library in C++?

    – FranzHuber23
    Mar 28 at 8:17






  • 1





    In native c++ you probably want to have a macro change from __declspec(dllexport) when building the dll to __declspec(dllimport) when using the dll. Here is a question that should help: stackoverflow.com/questions/14980649/…

    – drescherjm
    Mar 28 at 13:18












  • Yes @J.R.......

    – TerribleDog
    Mar 29 at 0:09






  • 1





    @FranzHuber23 Use an external DLL in c++ that was also used in c# project

    – TerribleDog
    Mar 29 at 0:09












  • 1





    Are you targeting the Windows platform?

    – J.R.
    Mar 28 at 4:56











  • What are you trying to do? Targeting a C# dll in C++? Or just including a C++ library in C++?

    – FranzHuber23
    Mar 28 at 8:17






  • 1





    In native c++ you probably want to have a macro change from __declspec(dllexport) when building the dll to __declspec(dllimport) when using the dll. Here is a question that should help: stackoverflow.com/questions/14980649/…

    – drescherjm
    Mar 28 at 13:18












  • Yes @J.R.......

    – TerribleDog
    Mar 29 at 0:09






  • 1





    @FranzHuber23 Use an external DLL in c++ that was also used in c# project

    – TerribleDog
    Mar 29 at 0:09







1




1





Are you targeting the Windows platform?

– J.R.
Mar 28 at 4:56





Are you targeting the Windows platform?

– J.R.
Mar 28 at 4:56













What are you trying to do? Targeting a C# dll in C++? Or just including a C++ library in C++?

– FranzHuber23
Mar 28 at 8:17





What are you trying to do? Targeting a C# dll in C++? Or just including a C++ library in C++?

– FranzHuber23
Mar 28 at 8:17




1




1





In native c++ you probably want to have a macro change from __declspec(dllexport) when building the dll to __declspec(dllimport) when using the dll. Here is a question that should help: stackoverflow.com/questions/14980649/…

– drescherjm
Mar 28 at 13:18






In native c++ you probably want to have a macro change from __declspec(dllexport) when building the dll to __declspec(dllimport) when using the dll. Here is a question that should help: stackoverflow.com/questions/14980649/…

– drescherjm
Mar 28 at 13:18














Yes @J.R.......

– TerribleDog
Mar 29 at 0:09





Yes @J.R.......

– TerribleDog
Mar 29 at 0:09




1




1





@FranzHuber23 Use an external DLL in c++ that was also used in c# project

– TerribleDog
Mar 29 at 0:09





@FranzHuber23 Use an external DLL in c++ that was also used in c# project

– TerribleDog
Mar 29 at 0:09












1 Answer
1






active

oldest

votes


















0















Provided that you do not have the development header and lib files available for the DLL and you need to dynamically load the DLL into your C++ project, then you can do the following.



Define a function pointer (equivalent to your extern declaration):



typedef int FnSomeFunction(const char* input);


Load the library (I'm using LoadLibraryA here to load an ansi-named DLL, this depends on your C++ project). The DLL must be in the search path, i.e. in the same path as the executable):



HMODULE hModule = LoadLibraryA("something.dll");


Check that the module is successfully loaded:



if (hModule == nullptr)
throw std::runtime_error("Lib not loaded");


Get the function entry point from the library:



FnSomething* fnSomething = (FnSomeFunction*)GetProcAddress(hModule, "somthng");


Call the function:



(*fnSomething)("some text");


Free the library when no longer needed:



FreeLibrary(hModule);





share|improve this answer



























  • Alternatively, just build your C++ project against the appropriate header (.h) and import library (.lib) so that you can just call the function directly. If you can link directly against the import library, it will be much more convenient if there are a lot of functions to import.

    – jazzdelightsme
    Mar 29 at 3:44











  • True; my assumption was that these were not available. I'll update the answer to reflect this.

    – J.R.
    Mar 29 at 3:53










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55388471%2fporting-dll-import-from-c-sharp-to-c%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0















Provided that you do not have the development header and lib files available for the DLL and you need to dynamically load the DLL into your C++ project, then you can do the following.



Define a function pointer (equivalent to your extern declaration):



typedef int FnSomeFunction(const char* input);


Load the library (I'm using LoadLibraryA here to load an ansi-named DLL, this depends on your C++ project). The DLL must be in the search path, i.e. in the same path as the executable):



HMODULE hModule = LoadLibraryA("something.dll");


Check that the module is successfully loaded:



if (hModule == nullptr)
throw std::runtime_error("Lib not loaded");


Get the function entry point from the library:



FnSomething* fnSomething = (FnSomeFunction*)GetProcAddress(hModule, "somthng");


Call the function:



(*fnSomething)("some text");


Free the library when no longer needed:



FreeLibrary(hModule);





share|improve this answer



























  • Alternatively, just build your C++ project against the appropriate header (.h) and import library (.lib) so that you can just call the function directly. If you can link directly against the import library, it will be much more convenient if there are a lot of functions to import.

    – jazzdelightsme
    Mar 29 at 3:44











  • True; my assumption was that these were not available. I'll update the answer to reflect this.

    – J.R.
    Mar 29 at 3:53















0















Provided that you do not have the development header and lib files available for the DLL and you need to dynamically load the DLL into your C++ project, then you can do the following.



Define a function pointer (equivalent to your extern declaration):



typedef int FnSomeFunction(const char* input);


Load the library (I'm using LoadLibraryA here to load an ansi-named DLL, this depends on your C++ project). The DLL must be in the search path, i.e. in the same path as the executable):



HMODULE hModule = LoadLibraryA("something.dll");


Check that the module is successfully loaded:



if (hModule == nullptr)
throw std::runtime_error("Lib not loaded");


Get the function entry point from the library:



FnSomething* fnSomething = (FnSomeFunction*)GetProcAddress(hModule, "somthng");


Call the function:



(*fnSomething)("some text");


Free the library when no longer needed:



FreeLibrary(hModule);





share|improve this answer



























  • Alternatively, just build your C++ project against the appropriate header (.h) and import library (.lib) so that you can just call the function directly. If you can link directly against the import library, it will be much more convenient if there are a lot of functions to import.

    – jazzdelightsme
    Mar 29 at 3:44











  • True; my assumption was that these were not available. I'll update the answer to reflect this.

    – J.R.
    Mar 29 at 3:53













0














0










0









Provided that you do not have the development header and lib files available for the DLL and you need to dynamically load the DLL into your C++ project, then you can do the following.



Define a function pointer (equivalent to your extern declaration):



typedef int FnSomeFunction(const char* input);


Load the library (I'm using LoadLibraryA here to load an ansi-named DLL, this depends on your C++ project). The DLL must be in the search path, i.e. in the same path as the executable):



HMODULE hModule = LoadLibraryA("something.dll");


Check that the module is successfully loaded:



if (hModule == nullptr)
throw std::runtime_error("Lib not loaded");


Get the function entry point from the library:



FnSomething* fnSomething = (FnSomeFunction*)GetProcAddress(hModule, "somthng");


Call the function:



(*fnSomething)("some text");


Free the library when no longer needed:



FreeLibrary(hModule);





share|improve this answer















Provided that you do not have the development header and lib files available for the DLL and you need to dynamically load the DLL into your C++ project, then you can do the following.



Define a function pointer (equivalent to your extern declaration):



typedef int FnSomeFunction(const char* input);


Load the library (I'm using LoadLibraryA here to load an ansi-named DLL, this depends on your C++ project). The DLL must be in the search path, i.e. in the same path as the executable):



HMODULE hModule = LoadLibraryA("something.dll");


Check that the module is successfully loaded:



if (hModule == nullptr)
throw std::runtime_error("Lib not loaded");


Get the function entry point from the library:



FnSomething* fnSomething = (FnSomeFunction*)GetProcAddress(hModule, "somthng");


Call the function:



(*fnSomething)("some text");


Free the library when no longer needed:



FreeLibrary(hModule);






share|improve this answer














share|improve this answer



share|improve this answer








edited Mar 29 at 3:55

























answered Mar 29 at 0:41









J.R.J.R.

1,2211 silver badge9 bronze badges




1,2211 silver badge9 bronze badges















  • Alternatively, just build your C++ project against the appropriate header (.h) and import library (.lib) so that you can just call the function directly. If you can link directly against the import library, it will be much more convenient if there are a lot of functions to import.

    – jazzdelightsme
    Mar 29 at 3:44











  • True; my assumption was that these were not available. I'll update the answer to reflect this.

    – J.R.
    Mar 29 at 3:53

















  • Alternatively, just build your C++ project against the appropriate header (.h) and import library (.lib) so that you can just call the function directly. If you can link directly against the import library, it will be much more convenient if there are a lot of functions to import.

    – jazzdelightsme
    Mar 29 at 3:44











  • True; my assumption was that these were not available. I'll update the answer to reflect this.

    – J.R.
    Mar 29 at 3:53
















Alternatively, just build your C++ project against the appropriate header (.h) and import library (.lib) so that you can just call the function directly. If you can link directly against the import library, it will be much more convenient if there are a lot of functions to import.

– jazzdelightsme
Mar 29 at 3:44





Alternatively, just build your C++ project against the appropriate header (.h) and import library (.lib) so that you can just call the function directly. If you can link directly against the import library, it will be much more convenient if there are a lot of functions to import.

– jazzdelightsme
Mar 29 at 3:44













True; my assumption was that these were not available. I'll update the answer to reflect this.

– J.R.
Mar 29 at 3:53





True; my assumption was that these were not available. I'll update the answer to reflect this.

– J.R.
Mar 29 at 3:53








Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.







Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.



















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55388471%2fporting-dll-import-from-c-sharp-to-c%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript