Windows VC++ 2010 link error _main - visual-c++

I was trying to poll available devices on a Windows machine by this code example from Microsoft. But there are link errors that relates to _main().
#include <stdio.h>
#include <windows.h>
#include <setupapi.h>
#include <devguid.h>
#include <regstr.h>
int main( int argc, char *argv[ ], char *envp[ ] )
{
HDEVINFO hDevInfo;
SP_DEVINFO_DATA DeviceInfoData;
DWORD i;
// Create a HDEVINFO with all present devices.
hDevInfo = SetupDiGetClassDevs(NULL,
0, // Enumerator
0,
DIGCF_PRESENT | DIGCF_ALLCLASSES );
if (hDevInfo == INVALID_HANDLE_VALUE)
{
// Insert error handling here.
return 1;
}
// Enumerate through all devices in Set.
DeviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
for (i=0;SetupDiEnumDeviceInfo(hDevInfo,i,
&DeviceInfoData);i++)
{
DWORD DataT;
LPTSTR buffer = NULL;
DWORD buffersize = 0;
//
// Call function with null to begin with,
// then use the returned buffer size (doubled)
// to Alloc the buffer. Keep calling until
// success or an unknown failure.
//
// Double the returned buffersize to correct
// for underlying legacy CM functions that
// return an incorrect buffersize value on
// DBCS/MBCS systems.
//
while (!SetupDiGetDeviceRegistryProperty(
hDevInfo,
&DeviceInfoData,
SPDRP_DEVICEDESC,
&DataT,
(PBYTE)buffer,
buffersize,
&buffersize))
{
if (GetLastError() ==
ERROR_INSUFFICIENT_BUFFER)
{
// Change the buffer size.
if (buffer) LocalFree(buffer);
// Double the size to avoid problems on
// W2k MBCS systems per KB 888609.
buffer = LocalAlloc(LPTR,buffersize * 2);
}
else
{
// Insert error handling here.
break;
}
}
printf("Result:[%s]\n",buffer);
if (buffer) LocalFree(buffer);
}
if ( GetLastError()!=NO_ERROR &&
GetLastError()!=ERROR_NO_MORE_ITEMS )
{
// Insert error handling here.
return 1;
}
// Cleanup
SetupDiDestroyDeviceInfoList(hDevInfo);
return 0;
}
For some reasons, there are link errors:
1>device.obj : error LNK2019: unresolved external symbol __imp__SetupDiDestroyDeviceInfoList#4 referenced in function _main
1>device.obj : error LNK2019: unresolved external symbol __imp__SetupDiGetDeviceRegistryPropertyW#28 referenced in function _main
1>device.obj : error LNK2019: unresolved external symbol __imp__SetupDiEnumDeviceInfo#12 referenced in function _main
1>device.obj : error LNK2019: unresolved external symbol __imp__SetupDiGetClassDevsW#16 referenced in function _main
1>c:\users\visual studio 2010\Projects\usb\Debug\usb.exe : fatal error LNK1120: 4 unresolved externals
What they are about? There is no _main() at all.

First thing first, the linker errors are not about missing main, but about other functions that are referenced (called) from main.
The functions you mentioned in linker errors are from library: Setupapi.lib, and you need to include it in Linker settings (Input) of your project.
Why did it worked on VS re-open?
Probably simply because you changed the configuration (by mistake). For example from Win32 to x64, and/or from Debug to Release (or any combination) of these. The other configuration didn't have reference to this library added.

Related

LNK2019 unable to use GetStagedPackageOrigin()

I have been trying to use the GetStagedPackageOrigin() to get the origin of a specific application in my system.
But my program is not compiling because of an unresolved external error.
Error LNK2019 : unresolved external symbol __imp__GetStagedPackageOrigin#8 referenced in function "void __cdecl check(wchar_t const *)" (?check##YAXPB_W#Z)
Here is my code:
void check(__in PCWSTR fullName)
{
PackageOrigin origin;
LONG rc = GetStagedPackageOrigin(fullName, &origin);
if (rc == ERROR_SUCCESS)
{
static PCWSTR originstring[] = {L"Unknown",L"Unsigned",L"Inbox",L"Store",L"DeveloperUnsigned",L"DeveloperSigned",L"LineOfBusiness" };
PCWSTR str = originstring[origin];
wcout << str << endl;
}
}
Why does this error keep coming and is there any way to resolve that?
I used dumpbin command in the visual studio command prompt and that function was not being exported from kernel32.dll. The function was not present in the dll in the first place.
I think that is why the error occured.

Unresolved externals in Visual C++

I'm trying to compile a solution in MS Visual Studio C++ 2012.
My code uses marshallsoft AES library.
I added these for library and include paths:
C:\aes4c\APPS to Configuration properties->VC++ Directories->Include Directories
C:\aes4c\DLLS to Configuration properties->VC++ Directories->Library Directories
When I compile the individual .cpp file it compiles without problem but when I build the solution I get:
------ Build started: Project: cryptest2, Configuration: Debug Win32 ------
cryptest2.obj : error LNK2019: unresolved external symbol __imp__aesAttach#8 referenced in function "int __cdecl EncryptFileW(char *,char *)" (?EncryptFileW##YAHPAD0#Z)
cryptest2.obj : error LNK2019: unresolved external symbol __imp__aesEncryptFile#12 referenced in function "int __cdecl EncryptFileW(char *,char *)" (?EncryptFileW##YAHPAD0#Z)
cryptest2.obj : error LNK2019: unresolved external symbol __imp__aesInitAES#20 referenced in function "int __cdecl EncryptFileW(char *,char *)" (?EncryptFileW##YAHPAD0#Z)
C:\Users\ariyan\documents\visual studio 2012\Projects\cryptest2\Debug\cryptest2.exe : fatal error LNK1120: 3 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
What is the problem?
How Can I fix it?
My code is:
#include "stdafx.h"
#include <windows.h>
#include <stdio.h>
#include "aes.h"
int EncryptFile(char *KeyBuffer, char *FileName);
int _tmain(int argc, _TCHAR* argv[])
{
EncryptFile("1234567890abcdef","c:\test.txt");
return 0;
}
int EncryptFile(char *KeyBuffer, char *FileName)
{int Code;
// attach DLL
Code = aesAttach(0, 0);
if(Code<0)
{printf("ERROR %d: Cannot attach\n", Code);
return FALSE;
}
printf("Will encrypt file in CBC mode\n");
Code = aesInitAES((char *)KeyBuffer, NULL, AES_ECB_MODE, AES_ENCRYPT, NULL);
if(Code<0)
{printf("aesInitAES fails\n");
return FALSE;
}
printf("Encrypt file...\n");
Code = aesEncryptFile(NULL, KeyBuffer, FileName);
if(Code<0)
{printf("aesEncryptFile fails\n");
return FALSE;
}
printf("%d bytes encrypted\n", Code);
return Code;
}
It's not enough to add to library path - that just tells the linker where to look for a library if and when it decides to link with it. But you have to tell the linker to look for it in the first place. For that, mention the LIB file name in
Project > Properties > Linker > Input > Additional Dependencies

VS2012 WindowsApplicationForDrivers8.0 can not include trd lib

#pragma comment(lib, "libmcrypt.lib")
When the platform is Visual Studio 2012 (v110), it is OK. But, when platform is WindowsApplicationForDrivers8.0, it reports error LNK2019
When I include OpenSSL in my project, I'm getting the same error.
From the MSDN.
The following sample generates LNK2019:
// LNK2019.cpp
// LNK2019 expected
extern char B[100]; // B is not in avilable to the linker
int main() {
B[0] = ' ';
}
LNK2019 can also occur when you declare but do not define a static data member. The following sample generates LNK2019:
// LNK2019b.cpp
// LNK2019 expected
struct C {
static int s;
};
// Uncomment the following line to resolve.
// int C::s;
int main() {
C c;
C::s = 1;
}
Please go to the MSDN link for full explanation
http://msdn.microsoft.com/en-us/library/799kze2z(v=vs.80).aspx

Unknown symbol when loading my own kernel module

The following code (pasted last), taken mostly from here, is a very simple kernel module which acts as a keylogger. I can get it to compile and produce a .ko just fine, but when I try to load it, I get the following errors in dmesg:
[ 790.833828] keylogger: Unknown symbol unregister_keyboard_notifier (err 0)
[ 790.833846] keylogger: Unknown symbol register_keyboard_notifier (err 0)
I did not build my kernel from source, but am using the stock kernel provided with archlinux. I did install the kernel-headers package to get the module to compile, however.
So my question is: Are these two symbols really not found in my installed kernel? And if they are, why aren't they linking(?) correctly?
I can find evidence that the symbols are present. Firstly, I can see the symbols in /proc/kallsyms. Also, when I do nm /usr/src/vmlinux I can also see these two symbols. Are they not the same?
Module code:
#include <linux/module.h> /* Needed by all modules */
#include <linux/keyboard.h>
EXPORT_SYMBOL_NOVERS(unregister_keyboard_notifier);
EXPORT_SYMBOL_NOVERS(register_keyboard_notifier);
int hello_notify(struct notifier_block *nblock, unsigned long code, void *_param) {
struct keyboard_notifier_param *param = _param;
struct vc_data *vc = param->vc;
int ret = NOTIFY_OK;
if (code == KBD_KEYCODE) {
printk(KERN_DEBUG "KEYLOGGER %i %s\n", param->value, (param->down ? "down" : "up"));
}
}
static struct notifier_block nb = {
.notifier_call = hello_notify
};
static int hello_init(void)
{
register_keyboard_notifier(&nb);
return 0;
}
static void hello_release(void)
{
unregister_keyboard_notifier(&nb);
}
module_init(hello_init);
module_exit(hello_release);
I needed to add the following to my module source:
MODULE_LICENSE("GPL");

Matlab / Microsoft Visual C++ 2010 Express: What is "error LNK2019: unresolved external symbol"?

I am trying to create a simple C++ class and a Matlab mex file. My code is as follows:
Matlab: mexTest1.cpp
#include "mex.h"
#include "K:\\My Documents\\Visual Studio 2010\\Projects\\HelloWorld\\HelloWorld\\Class1.h"
/* Input Arguments */
#define X prhs[0]
/* Output Arguments */
#define RESULT plhs[0]
void mexFunction( int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[] ){
/* Initialize input and output pointers
// Inputs */
double *x;
/* Outputs */
double r;
double *result;
mwSize m,n;
m = mxGetM(X);
n = mxGetN(X);
/* Create a matrix for the return argument */
RESULT = mxCreateDoubleMatrix(1, 1, mxREAL);
/* Assign pointers to the various parameters */
result = mxGetPr(RESULT);
x = mxGetPr(X);
/* Do the actual computations in a subroutine */
Class1 c1(2, 15.0);
r = c1.product();
result[0] = r;
return;
}
Class1.h:
#pragma once
#include <string> // Standard string class in C++
class Class1
{
public:
int a;
double b;
public:
Class1(const int& a, const double& b);
//virtual ~Class1();
void print() const;
double product() const;
};
Class1.cpp:
#include "stdafx.h"
#include <iostream>
#include "Class1.h"
Class1::Class1(const int& a, const double& b){
Class1::a = a;
Class1::b = b;
}
void Class1::print() const{
std::cout << "a=" << Class1::a << " * b=" << Class1::b << " = " << Class1::product() << std::endl;
}
double Class1::product() const{
return a*b;
}
Running the Matlab command mex mexTest1.cpp gives the error messages:
Creating library C:\DOCUME~1\K\LOCALS~1\TEMP\MEX_RH~1\templib.x and object C:\DOCUME~1\K\LOCALS~1\TEMP\MEX_RH~1\templib.exp
mexTest1.obj : error LNK2019: unresolved external symbol "public: double __thiscall Class1::product(void)const " (?product#Class1##QBENXZ) referenced in function _mexFunction
mexTest1.obj : error LNK2019: unresolved external symbol "public: __thiscall Class1::Class1(int const &,double const &)" (??0Class1##QAE#ABHABN#Z) referenced in function _mexFunction
mexTest1.mexw32 : fatal error LNK1120: 2 unresolved externals
C:\PROGRA~1\MATLAB\R2011A\BIN\MEX.PL: Error: Link of 'mexTest1.mexw32' failed.
??? Error using ==> mex at 208
Unable to complete successfully.
Can anyone help me fix this problem?
Thanks.
The linker is telling you that in trying to construct an executable, it wasn't supplied with an object file that contains Class1::product and Class1::Class1. That's because those functions would be supplied by compiling Class1.cpp, which your command line doesn't ask for.
You should use the syntax of mex for multiple files: mex mexTest1.cpp Class1.cpp
Your linker is unable to find deinitions(bodies) Class1 methods (constructor and product). This may be due to
You haven't provided any definition(body)
The definitions are in a lib file which you forgot to link to

Resources