I have a DLL which is develeoped in VB.Net. I am trying to call its functions from my vc++ code. The dll has successfully loaded using LoadLibrary function.
But when I try calling any function within the dll, it gives a null pointer exception.
I used the dumpbin command to confirm the function arguments within my dll. But it is not listing any functions. Could it be a problem with the dll or does dumpbin support few dlls only? Please help!
C:\Program Files (x86)\Microsoft Visual Studio 11.0>dumpbin ECR.dll
Microsoft (R) COFF/PE Dumper Version 11.00.51106.1 Copyright (C)
Microsoft Corporation. All rights reserved.
Dump of file ECR.dll
File Type: DLL
Summary
2000 .reloc
4000 .rsrc
2000 .sdata
16000 .text
Try writing before any function in your DLL file (header .h files) the name of the project with _API at the end (ECR_API).
for example, lets say we want to create a constructor and destructor for a class called Loader:
class Loader{
public:
ECR_API Loader();
ECR_API ~Loader();
}
also dont forget to add export and import statements at the beggining of your header file:
#ifdef ECR_EXPORTS
#define ECR_API __declspec(dllexport)
#else
#define ECR __declspec(dllimport)
#endif
hope this helps!
worked fine for me.
Related
I have been looking at the example code from Microsoft for using WinHTTP over COM, see: https://learn.microsoft.com/en-us/windows/win32/winhttp/iwinhttprequest-open
The first few lines of the code are as follows:
#include <windows.h>
#include <stdio.h>
#include <objbase.h>
#include "httprequest.h"
#pragma comment(lib, "ole32.lib")
#pragma comment(lib, "oleaut32.lib")
// IID for IWinHttpRequest.
const IID IID_IWinHttpRequest =
{
0x06f29373,
0x5c5a,
0x4b54,
{0xb0, 0x25, 0x6e, 0xf1, 0xbf, 0x8a, 0xbf, 0x0e}
};
int main()
{
// Variable for return value
HRESULT hr;
// Initialize COM
hr = CoInitialize( NULL );
IWinHttpRequest * pIWinHttpRequest = NULL;
I am confused with the correct way to #include the IWinHttpRequest type. I think it comes from the httprequest.h file which is not a system include file.
Also, I guess httprequest.h is the result of compiling httprequest.idl. Am I supposed to manually compile httprequest.idl, or as this is part of the Windows SDK is there a better way to access this type?
There are just missing instructions on how to get the httprequest.h file. The Windows SDK only has the .idl file. This is somehow unusual, many Windows APIs have the .idl and the .h, some only have .h, some only have .idl ... But you can create the .h from the .idl:
open Visual Studio's developer command prompt
type 'midl httprequest.idl /out [your project path]'
This is a sample output:
**********************************************************************
** Visual Studio 2019 Developer Command Prompt v16.5.1
** Copyright (c) 2019 Microsoft Corporation
**********************************************************************
C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional>midl httprequest.idl /out D:\MyPath\MyAppDir
Microsoft (R) 32b/64b MIDL Compiler Version 8.01.0622
Copyright (c) Microsoft Corporation. All rights reserved.
Processing C:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\um\httprequest.idl
httprequest.idl
Processing C:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\um\oaidl.idl
oaidl.idl
Processing C:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\um\objidl.idl
objidl.idl
Processing C:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\um\unknwn.idl
unknwn.idl
Processing C:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\shared\wtypes.idl
wtypes.idl
Processing C:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\shared\wtypesbase.idl
wtypesbase.idl
Processing C:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\shared\basetsd.h
basetsd.h
Processing C:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\shared\guiddef.h
guiddef.h
Processing C:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\um\oaidl.acf
oaidl.acf
It will create 3 files: httprequest.h, httprequest.tlb and httprequest_i.c in your project directory.
Now you should be able to compile the sample code. Note if you include the httprequest_i.c file, you can even remove the manual declaration of IID_IWinHttpRequest in the sample.
Well, knowing absolutely nothing about this beforehand, I looked up the interface you gave, IWinHttpRequest. I went to the registry and searched for it in HKCR\Interface, found the typelib for the interface in the registry. The typelib is HKEY_CLASSES_ROOT\TypeLib\{662901FC-6951-4854-9EB2-D9A2570F2B2E}. From the typelib, looked up HKEY_CLASSES_ROOT\TypeLib\{662901FC-6951-4854-9EB2-D9A2570F2B2E}\5.1\0\win32. The win32 entry shows where the location of the typelib on disk is: %SystemRoot%\system32\winhttpcom.dll
Since the %SystemRoot%\system32 directly is always in the path, you can use the typelib simmply like this:
#import "winhttpcom.dll"
Behind the scenes, the Visual C++ compiler will create files named winhttpcom.tlh and winhttpcom.tli. You can ignore those files. The compiler just automatically includes and links with those files.
It will declare COM smart pointers in the .tlh file:
_COM_SMARTPTR_TYPEDEF(IWinHttpRequest, __uuidof(IWinHttpRequest));
_COM_SMARTPTR_TYPEDEF(IWinHttpRequestEvents, __uuidof(IWinHttpRequestEvents));
All of this will be wrapped in a C++ namespace:
namespace WinHttp {
// definitions in here
}
If you don't want namespaces, you can do something like:
#import "winhttpcom.dll" no_namespace
Documentation for #import is at https://learn.microsoft.com/en-us/cpp/preprocessor/hash-import-directive-cpp?view=vs-2019
Hello Professional Developer Community.
I'm a C# and Android developer. I don't understand much from C++.
I have a C++ Win32 project. I think it's written with VS 2015.
Project have two static libraries (LuaLibDll.lib and LuaScriptVM.lib)
Lib files have been added to the Linker>Input and have header files(.h).
The compilation is successful but gives me an error when starting the Win32 program.
The program can't start because LuaLib.dll (and LuaScriptMV.dll) is
missing from your computer.Try reinstalling the program to fix this
problem.
Note:
Project does not create dll(s).
These codes are added in LuaLib.h and LuScriptVM.h
#ifndef __linux
#ifdef LUASCRIPTVM_EXPORTS
#define _LUAVM __declspec(dllexport)
#else
#define _LUAVM __declspec(dllimport)
#endif
#else
#define _LUAVM
#endif
Thank you for your interest
I need to compile a mex file which is written in C++ (VS 2008) with Matlab 2009. If I compile with VS 2008 directly, it seems Matlab is not compatible and the error is:
//Invalid MEX-file
So I tried to comile with Matlab directly using MEX function. In "mex -setup" I chose
//Lcc-win32 C 2.4.1 in C:\PROGRA~1\MATLAB\R2009a\sys\lcc
to compile with Matlab, but it only compile C and not C++ and my codes are in C++.
Then I chose
//Microsoft Visual C++ 2008 SP1 in C:\Programme\Microsoft Visual Studio 9.0
to compile with VS 2008 from Matlab. For a simple test C++ file it worked. The mex file was perfect. Then I added some header files and a function to call a class in one of the header files. I added "Aria" header files to my project and the above error comes.
//Aria website where you can find header files: (http://robots.mobilerobots.com/wiki/ARIA)
Header files are in the same directory as main program, all the lib files are defined in VC++ project. In "Additional library directories" I added their path again. When I build my project through VC++, there is no error. But when I try to use MEX from Matlab, this error happens. (the name of C++ file is "test")
//
Microsoft (R) Incremental Linker Version 9.00.21022.08
Copyright (C) Microsoft Corporation. All rights reserved.
...
test.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: static void _cdecl Aria::init(enum Aria::SigHandleMethod,bool,bool)" (_imp_?init#Aria##SAXW4SigHandleMethod#1#_N1#Z) referenced in function _main
test.mexw32 : fatal error LNK1120: 1 unresolved externals
C:\PROGRA~1\MATLAB\R2009A\BIN\MEX.PL: Error: Link of 'test.mexw32' failed.
??? Error using ==> mex at 218
Unable to complete successfully.
//
To test the project, I wrote a small class in one of that header files and used that class in C++ file, and it worked! I do not know why other classes do not work. They are linked to each other and they are about 140 .h files.
Has anyone any idea why this happens?
Why there is no error when I build, release and debug project in the VC++, but when I try to compile MEX file with Matlab, this error?
All the header files and necessary libs needs to be compiled.
//mex name.cpp name.h name.lib
Here, WinMM.Lib, WSock32.Lib and AdvAPI32.Lib.
I'm trying to build a project (for ARM) to facilitate the communication over an HID device. Here are the details of what I have:
PC with Windows 7
Visual Studio 2011 Developer Preview
WDK
A microsoft library function FindKnownHidDevices() makes a call to HidD_GetHidGuid() in my project. However, Linker doesn't like this and complains:
*error LNK2019: unresolved external symbol HidD_GetHidGuid referenced in function "unsigned char __cdecl FindKnownHidDevices*
I already have all of following workarounds in my project with no success so far:
Include hid.lib & SetupAPI.lib in Linker -> Input -> Additional Dependencings under configuration manager of VS 2011
Include hidsdi.h under "VC++ Directories -> Include Directories" in Configuration Manager of VS 2011
Include hidsdi.h in the .h version of .c file which makes a call to the function HidD_GetHidGuid() using extern "C"
What might I be missing?
If the code compiles OK, then the compiler is finding the correct header files. As the linker is failing, this suggests that it cannot find the specified .lib files.
Have you downloaded and installed the WoA SDK? The required lib files may not actually be in the linker folder for ARM targets (they're not in the default installation for Studio 11 Developer Preview).
VS C++ 2008
I am just working through a DirectX tutorial.
In the source code had this line:
#pragma comment (lib, "d3d9.lib")
When I compiled everything linked ok.
However, I commented out this line and tried to include the header and library myself under properties, like this:
C/C++ - General
Additional include directories: "C:\Program Files\Microsoft DirectX SDK (August 2009)\Include"
Linker - General
Additional library directories: "C:\Program Files\Microsoft DirectX SDK (August 2009)\Lib\x64"
Linker - Input: d3d9.lib
However, I got this linker error:
1>main.obj : error LNK2019: unresolved external symbol _Direct3DCreate9#4 referenced in function _initD3D
However, when I just use the pragma I didn't get any linker errors. Only when I try and include them with the properties as above.
What is the real difference in using pragma and including the header/libraries using the properites?
Many thanks,
at first, #pragma comment(lib) is just linker configuration
at second, the SDK should be in path, so dont set additional library directories (you may override it with wrong version), just add d3d9.lib to linker's input.
As far as I know, there is no difference. pragma lib simply says to the linker to look for a specific library by name.
Also, since the path is not specified in the pragma, the linker relies on the current lib paths for your project. Try not add any path to your linker options (by default DX SDK adds paths to any visual studio installed, directly modifying the global visual studio paths. See Tools/Options/Projects and Solutions/VC++ Directories/Show Directories for Library files)
Some things to check:
you are indeed building for x64
your path is really pointing to the DX SDK (it is installed to Program Files(x86) if you are on x64)
verify if there are not other linker warnings