Question

As the subject says, what I'm trying to do is similar to this but using Visual Studio 2012.

I can build and produce a DLL, and I can load that DLL in javascript, but I cannot access the function in that DLL. Looking at the DLL in DllExp shows no functions, suggesting something's wrong with the DLL.

The DLL is a new C++ project created using the "Empty Project" template. Notable settings are;

General->Configuration Type set to DLL
No optimization.
No precompiled headers.
Compile as C code
Calling convention __cdecl

The commandlines for compilation and linking, in case there's a setting I've not thought significant, are

 /GS /TC /analyze- /W3 /Zc:wchar_t /ZI /Gm /Od /sdl /Fd"Debug\vc110.pdb" /fp:precise /D "_WINDLL" /D "_MBCS" /errorReport:prompt /WX- /Zc:forScope /RTC1 /Gd /Oy- /MDd /Fa"Debug\" /EHsc /nologo /Fo"Debug\" /Fp"Debug\StreamInterop.pch" 

And for linker

 /OUT:"C:\Work\VehicleTracker-DotNet\StreamInterop\Debug\StreamInterop.dll" /MANIFEST /NXCOMPAT /PDB:"C:\Work\VehicleTracker-DotNet\StreamInterop\Debug\StreamInterop.pdb" /DYNAMICBASE "kernel32.lib" "user32.lib" "gdi32.lib" "winspool.lib" "comdlg32.lib" "advapi32.lib" "shell32.lib" "ole32.lib" "oleaut32.lib" "uuid.lib" "odbc32.lib" "odbccp32.lib" /IMPLIB:"C:\Work\VehicleTracker-DotNet\StreamInterop\Debug\StreamInterop.lib" /DEBUG /DLL /MACHINE:X86 /INCREMENTAL /PGD:"C:\Work\VehicleTracker-DotNet\StreamInterop\Debug\StreamInterop.pgd" /MANIFESTUAC:"level='asInvoker' uiAccess='false'" /ManifestFile:"Debug\StreamInterop.dll.intermediate.manifest" /ERRORREPORT:PROMPT /NOLOGO /TLBI

The project contains a single C source file, main.c, containing

 #include<stdio.h>
 int add(int a,int b)
 {
     return(a+b);
 }

Given this is all that was needed in the earlier post, it seems to me the problem must be in the compiler or linker switches. Can anyone see what I'm missing?

Was it helpful?

Solution

On Windows using Visual Studio, to export a function from a DLL, use:

 #include<stdio.h>
 __declspec(dllexport) int add(int a,int b)
 {
     return(a+b);
 }

As a side note, don't choose Empty Project so Visual Studio will generate some example code for you.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top