DLL and linking problems maybe

Hello evryone

firs of all what i am trying to do is to make a dll which hooks a specific process. when i write the code in one .cpp file all is fine, but when i try to make the same program with a dll logic everything goes to the... hole.

so my structure of file of the dll is as follows
Headers Files
FOO.h
stdafx.h
targetver.h
Source Files
dllmain.cpp
FOO.cpp

the code in FOO.h:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
  #ifdef INJECTION_DEF
#define INJ_IMP_EXP __declspec(dllimport)
#else
#define INJ_IMP_EXP __declspec(dllexport)
#endif


#include "stdafx.h"
#include <Windows.h>
#include "madCHook.h"
#include <stdlib.h>
#include <TlHelp32.h>
#include <iostream>
#include "psapi.h"

typedef BOOL (WINAPI *TerminateProcessNext)(HANDLE procHandle, UINT exitCode);
TerminateProcessNext k;
INJ_IMP_EXP BOOL ThisIsOurProcess(DWORD procHandle);
INJ_IMP_EXP BOOL WINAPI TerminateProcessCallback(HANDLE procHandle, UINT exitCode);



the code in FOO.cpp
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
// FOO.cpp : Defines the exported functions for the DLL application.
//
#include "stdafx.h"

#include "FOO.h"

using namespace std;


DWORD getPid(string procName)
{
	HANDLE hsnap;
	PROCESSENTRY32 pt;
	hsnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
	pt.dwSize = sizeof(PROCESSENTRY32);
	wstring wsProcName = wstring(procName.begin(),procName.end()); //pravish conversion zaradi if-a vav do while cikala
   do
   {
	   if(!wcscmp(pt.szExeFile, wsProcName.c_str()))
	   {
		   DWORD pid = pt.th32ProcessID;
           CloseHandle(hsnap);
           return pid;
	   }
   }
   while(Process32Next(hsnap, &pt));
   CloseHandle(hsnap);
   return 0;          
}

string getPname(DWORD PID)
{
	HANDLE hsnap;
	PROCESSENTRY32 pt;
	hsnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
	pt.dwSize = sizeof(PROCESSENTRY32);
	Process32Next(hsnap, &pt);
	//wstring wsProcName = wstring(procName.begin(),procName.end()); //pravish conversion zaradi if-a vav do while cikala
   do
   {
	   if(PID == pt.th32ProcessID )
	   {
		   wstring strp (pt.szExeFile);
		   string pname (strp.begin(),strp.end());
 		   //DWORD pid = pt.th32ProcessID;
           CloseHandle(hsnap);
           return pname;
	   }
   }
   while(Process32Next(hsnap, &pt));
   CloseHandle(hsnap);
   return 0;          
}

BOOL ThisIsOurProcess(HANDLE procHandle)
{
	DWORD PID;
	
	PID = GetProcessId(procHandle);
	string arrCh = getPname(PID);
	BOOL h = arrCh.compare("notepad++.exe");
	return (PID!=0 && !h );
}

BOOL WINAPI TerminateProcessCallback(HANDLE procHandle, UINT exitCode)
{
	
	MessageBox(0,TEXT("sometext"),TEXT("nothingspecial"),MB_OK);
	if((int)procHandle>0 && ThisIsOurProcess(procHandle))
	{
		return false;
	}
	else
	{
		return k(procHandle,exitCode);
	}
}


and the code in dllmain.cpp
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
// dllmain.cpp : Defines the entry point for the DLL application.
#include "stdafx.h"
#include "FOO.h"

BOOL f;

BOOL APIENTRY DllMain( HMODULE hModule,
                       DWORD  ul_reason_for_call,
                       LPVOID lpReserved
					 )
{
	

	
	switch (ul_reason_for_call)
	{
	case DLL_PROCESS_ATTACH:
	case DLL_THREAD_ATTACH:
		f = HookAPI("kernel32.dll","TerminateProcess",TerminateProcessCallback,(PVOID*)&k);
		if(f)
		{
			std::cout<<"success\n";
			//TerminateProcess(hproc,exCODE);
		}
		else
		{
			std::cout<<"fuckoff\n" << GetLastError();
		}
	break;
	case DLL_THREAD_DETACH:
	case DLL_PROCESS_DETACH:
		break;
	}
	return TRUE;
}



the output after trying to build it is this
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
1>InitializeBuildStatus:
1>  Touching "Debug\FOO.unsuccessfulbuild".
1>ClCompile:
1>  All outputs are up-to-date.
1>  dllmain.cpp
1>  Skipping... (no relevant changes detected)
1>  FOO.cpp
1>ManifestResourceCompile:
1>  All outputs are up-to-date.
1>FOO.obj : error LNK2005: "int (__stdcall* k)(void *,unsigned int)" (?k@@3P6GHPAXI@ZA) already defined in dllmain.obj
1>madCHook32.lib(madCHook.obj) : warning LNK4202: spawning full build due to '/INCREMENTAL:NO' directive
1>dllmain.obj : warning LNK4075: ignoring '/EDITANDCONTINUE' due to '/INCREMENTAL:NO' specification
1>FOO.obj : error LNK2005: "int (__stdcall* k)(void *,unsigned int)" (?k@@3P6GHPAXI@ZA) already defined in dllmain.obj
1>LINK : fatal error LNK1149: output filename matches input filename 'E:\Projects\FOO\Debug\FOO.lib'
1>
1>Build FAILED.


and i can't understand why is it giving me error lnk2005.
I searched the internet about what to do but without a solution how to fix it.
Guess trying to call a function in dll which is in another .cpp and .h file doesn't work. I also played with the properties in the Linker section in General /AdditionalLibraryDirectories and the Input/AdditionalDependencies but that doesnt help either.
I will be very thankfull if someone help /explain whats wrong and how can i fix it.
make sure INJECTION_DEF is define in your project settings preprocessor
Sorry i am stupid as hell
didn't add any guard at all in the .h file
Topic archived. No new replies allowed.