Dependencies and errors when using atoi - c++

I was given the task to refactor a very old project, and in these current days i'm checking the dependencies of the executables because for some reasons, they changed since 2009, passing from 4 to 14. To be more specific, my job is to keep the dependencies as they were before 2009, but with the changes to the code occuring until today.
I tracked down the instruction that was causing the trouble. It'a function inside a library used by the project:
chain(str, pps)
char *pps;
char *str;
{
int pp = 0;
pp = atoi(pps);
// ic sunt leones.
If i comment or replace atoi with an assignment of an integer like 0, 1 or 3, the library compile fine, but the executable that is using this .lib gives me these errors:
nafxcw.lib(wincore.obj) : error LNK2001: unresolved external symbol __imp__InitCommonControls#0
nafxcw.lib(wincore.obj) : error LNK2001: unresolved external symbol __imp__DragAcceptFiles#8
nafxcw.lib(appcore.obj) : error LNK2001: unresolved external symbol _ClosePrinter#4
nafxcw.lib(appcore.obj) : error LNK2001: unresolved external symbol _DocumentPropertiesA#24
nafxcw.lib(appcore.obj) : error LNK2001: unresolved external symbol _OpenPrinterA#12
nafxcw.lib(filecore.obj) : error LNK2001: unresolved external symbol __imp__SHGetFileInfoA#20
nafxcw.lib(filecore.obj) : error LNK2001: unresolved external symbol _GetFileTitleA#12
If otherwise i use a different value for the assignment, like 2, 4 or every other integer, everything compile correctly and work.
Any advice? What's happening here? Why this strange behaviour?
EDIT: apparently the problem is not the atoi. If i use an home made function that do anything and accept a char* and return an int or replacing directly the second parameter of the function chain with an int and assigning it directly i still receive the same errors.

You can try this :
include ('limits.h');
int my_getnbr(char *str)
{
int i;
long nbr;
int neg;
neg = 0;
nbr = 0;
i = 0;
if (str[0] == '-')
{
neg = 1;
str++;
}
while (str[i] >= '0' && str[i] <= '9')
{
nbr = nbr * 10 + (str[i++] - '0');
if (nbr > INT_MAX)
return (0);
}
return (neg ? (int)nbr * -1 : (int)nbr);
}
It's just a home made atoi like.
Edit : INT_MAX by Alter Mann.
Edit bis : if (nbr > INT_MAX) by Alter Mann again :)

the only thing I can think of is that the simplified function is being compiled out of existence, and the linker is then helpfully assuming that there is no code to link with in the library, and so it is being dropped, unfortunately it being the only thing that is forcing includes of those MFC libraries.
I have no idea why its even requiring stuff like OpenPinter - which is included in winspool.h (via windows.h) except to say that it sounds like you have a nasty MFC-based mess to sort out. It was never much good WRT tidy builds due to its dependence on specific includes specified in specific order.

Related

unresolved external symbols in Detours Library? (Detours::X64::DetourFunction, Direct3DCreate9)

so i was following along an dll injection tutorial on yt, and it gave me some errors when trying to compile.
1.unresolved external symbol "unsigned __int64 __cdecl Detours::X64::DetourFunction(unsigned __int64,unsigned __int64,enum Detours::X64Option)" (?DetourFunction#X64#Detours##YA_K_K0W4X64Option#2##Z) referenced in function "unsigned long __cdecl mainThread(void *)"
CODE TO ERROR:
DWORD WINAPI mainThread(PVOID base) {
void* d3d9Device[119];
if(GetD3D9Device(d3d9Device, sizeof(d3d9Device))){
oEndScene = (EndScene)Detours::X64::DetourFunction((uintptr_t)d3d9Device[42], (uintptr_t)hkEndScene);//here
}
while (true) {
if (GetAsyncKeyState(VK_F10)) {
CleanUpDeviceD3D();
FreeLibraryAndExitThread(static_cast<HMODULE>(base),1);
}
}
FreeLibraryAndExitThread(static_cast<HMODULE>(base), 1);
}
2. unresolved external symbol Direct3DCreate9 referenced in function "bool __cdecl GetD3D9Device(void * *,unsigned __int64)"
CODE TO ERROR:
bool GetD3D9Device(void **pTable, size_t size) {
if (!pTable) {
return false;
}
g_pD3D = Direct3DCreate9(D3D_SDK_VERSION); //here
on the first error, the tutorial stated you should use the following syntax:
oEndScene = (EndScene)Detours::X64::DetourFunction((Detours::uint8_t*)d3d9Device[42], (Detours::uint8_t*)hkEndScene);
however detours doesnt have uint8_t for me and some forums online said to use uintptr_t. but i still get the error. ive tried looking at my includes for lib and release in detours but it still says unresolved external symbol even after including the folder where Detoursx64.cpp is present.
any help ise appreciated.
you need to compile Detours as x86 and use the release folder instead of compiling detours as x64

Error while building MFC 4.1 project in MFC 6.0

I downloaded a project which was originally developed in MFC 4.1. I'm trying to build that project in MFC 6.0. When I opened it using Open WorkSpace, and build it, then following error came.
Link : fatal error LNK1104: cannot open file "mfc42ud.lib"
On internet I found a solution to fix this error by ignoring this library from Project settings. I did that and then another error on similar lines appeared for mfcs42ud.lib file. And I fixed that too.
When I added both the .lib files to ignore list, and build it, then I'm getting 901 errors like below....
--------------------Configuration: doodads - Win32 Unicode Debug-------------------- Linking... doodads.obj : error LNK2001:
unresolved external symbol __afxForceEXCLUDE ADDRESS.OBJ : error
LNK2001: unresolved external symbol __afxForceEXCLUDE expose.obj :
error LNK2001: unresolved external symbol __afxForceEXCLUDE
progress.obj : error LNK2001: unresolved external symbol
__afxForceEXCLUDE MaskDlg.obj : error LNK2001: unresolved external symbol __afxForceEXCLUDE SETKEY.OBJ : error LNK2001: unresolved
external symbol __afxForceEXCLUDE SPINNER.OBJ : error LNK2001:
unresolved external symbol __afxForceEXCLUDE stats.obj : error
LNK2001: unresolved external symbol __afxForceEXCLUDE ANIME.OBJ :
error LNK2001: unresolved external symbol __afxForceEXCLUDE STDAFX.OBJ
: error LNK2001: unresolved external symbol __afxForceEXCLUDE
MAINFRM.OBJ : error LNK2001: unresolved external symbol
__afxForceEXCLUDE DODADDOC.OBJ : error LNK2001: unresolved external symbol __afxForceEXCLUDE DoDadvw.obj : error LNK2001: unresolved
external symbol __afxForceEXCLUDE doodads.obj : error LNK2001:
unresolved external symbol __afxForceSTDAFX ADDRESS.OBJ : error
LNK2001: unresolved external symbol __afxForceSTDAFX expose.obj :
error LNK2001: unresolved external symbol __afxForceSTDAFX
progress.obj : error LNK2001: unresolved external symbol
__afxForceSTDAFX MaskDlg.obj : error LNK2001: unresolved external symbol __afxForceSTDAFX SETKEY.OBJ : error LNK2001: unresolved
external symbol __afxForceSTDAFX SPINNER.OBJ : error LNK2001:
unresolved external symbol __afxForceSTDAFX stats.obj : error LNK2001:
unresolved external symbol __afxForceSTDAFX ANIME.OBJ : error LNK2001:
unresolved external symbol __afxForceSTDAFX STDAFX.OBJ : error
LNK2001: unresolved external symbol __afxForceSTDAFX MAINFRM.OBJ :
error LNK2001: unresolved external symbol __afxForceSTDAFX
DODADDOC.OBJ : error LNK2001: unresolved external symbol
__afxForceSTDAFX DoDadvw.obj : error LNK2001: unresolved external symbol __afxForceSTDAFX
Here is the code file for your reference.
Add.h
// ADD.H - Header file for your Internet Server
// add Extension
#include "resource.h"
class CAddExtension : public CHttpServer
{
public:
CAddExtension();
~CAddExtension();
// Overrides
// ClassWizard generated virtual function overrides
// NOTE - the ClassWizard will add and remove member functions here.
// DO NOT EDIT what you see in these blocks of generated code !
//{{AFX_VIRTUAL(CAddExtension)
public:
virtual BOOL GetExtensionVersion(HSE_VERSION_INFO* pVer);
//}}AFX_VIRTUAL
// TODO: Add handlers for your commands here.
// For example:
void Default(CHttpServerContext* pCtxt,
LPCTSTR pstrOp1, LPCTSTR pstrOp2);
DECLARE_PARSE_MAP()
//{{AFX_MSG(CAddExtension)
//}}AFX_MSG
};
Add.cpp
// ADD.CPP - Implementation file for your Internet Server
// add Extension
#include "stdafx.h"
#include "add.h"
#include <stdio.h>
///////////////////////////////////////////////////////////////////////
// command-parsing map
BEGIN_PARSE_MAP(CAddExtension, CHttpServer)
// TODO: insert your ON_PARSE_COMMAND() and
// ON_PARSE_COMMAND_PARAMS() here to hook up your commands.
// For example:
ON_PARSE_COMMAND(Default, CAddExtension, ITS_PSTR ITS_PSTR)
ON_PARSE_COMMAND_PARAMS("opone=~ optwo=~")
DEFAULT_PARSE_COMMAND(Default, CAddExtension)
END_PARSE_MAP(CAddExtension)
///////////////////////////////////////////////////////////////////////
// The one and only CAddExtension object
CAddExtension theExtension;
///////////////////////////////////////////////////////////////////////
// CAddExtension implementation
CAddExtension::CAddExtension()
{
}
CAddExtension::~CAddExtension()
{
}
BOOL CAddExtension::GetExtensionVersion(HSE_VERSION_INFO* pVer)
{
// Call default implementation for initialization
CHttpServer::GetExtensionVersion(pVer);
// Load description string
TCHAR sz[HSE_MAX_EXT_DLL_NAME_LEN+1];
ISAPIVERIFY(::LoadString(AfxGetResourceHandle(),
IDS_SERVER, sz, HSE_MAX_EXT_DLL_NAME_LEN));
_tcscpy(pVer->lpszExtensionDesc, sz);
return TRUE;
}
///////////////////////////////////////////////////////////////////////
// CAddExtension command handlers
void CAddExtension::Default(CHttpServerContext* pCtxt,
LPCTSTR pstrOp1, LPCTSTR pstrOp2)
{
StartContent(pCtxt);
WriteTitle(pCtxt);
int nOp1;
int nOp2;
if (*pstrOp1 != '~' && *pstrOp2 != '~')
{
nOp1 = atoi(pstrOp1);
nOp2 = atoi(pstrOp2);
char sz[1024];
sprintf(sz, "The result is: %d\n", nOp1 + nOp2);
*pCtxt << sz;
}
else
{
*pCtxt << "<FORM ACTION=\"http://mooseboy/add.dll\" METHOD=\"GET\">";
*pCtxt << "Enter the first number: <INPUT TYPE=\"text\" NAME=\"opone\" VALUE=\"0\" SIZE=10><p>";
*pCtxt << "Enter the second number: <INPUT TYPE=\"text\" NAME=\"optwo\" VALUE=\"0\" SIZE=10><p>";
*pCtxt << "<p> <INPUT TYPE = \"SUBMIT\" VALUE=\"Add it up\">";
*pCtxt << "</FORM>";
}
EndContent(pCtxt);
}
// Do not edit the following lines, which are needed by ClassWizard.
#if 0
BEGIN_MESSAGE_MAP(CAddExtension, CHttpServer)
//{{AFX_MSG_MAP(CAddExtension)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
#endif // 0
///////////////////////////////////////////////////////////////////////
// If your extension will not use MFC, you'll need this code to make
// sure the extension objects can find the resource handle for the
// module. If you convert your extension to not be dependent on MFC,
// remove the comments arounn the following AfxGetResourceHandle()
// and DllMain() functions, as well as the g_hInstance global.
static HINSTANCE g_hInstance;
HINSTANCE AFXISAPI AfxGetResourceHandle()
{
return g_hInstance;
}
BOOL WINAPI DllMain(HINSTANCE hInst, ULONG ulReason,
LPVOID lpReserved)
{
if (ulReason == DLL_PROCESS_ATTACH)
{
g_hInstance = hInst;
}
return TRUE;
}
Just for information, I've marked Microsoft Foundation Classes to Use MFC in a shared DLL under General tab. Please help, TIA.
MFC 6.0 uses MFC42*.LIB library files (strange but true). So it seems you link with the correct filenames.
If you get these error messages when you link your project, I guess that either your project file has something wrong, or Visual C++ 6 is not correctly installed. The solution is not to remove these libraries, but to find out why Visual C++ doesn't find them.
Your project is compiling in unicode-debug configuration. Have you installed the unicode libraries when you have installed Visual C++? Maybe this is an opt-in install option (not sure). Can you build your project using another configuration, like release-unicode? Or Debug?
What can help you is to create a new empty Visual C++/MFC project (File/New Project/C++/MFC/MFC Application), so will get a valid MFC project configuration. Be sure to choose Unicode configuration, because this is the configuration you want to use.

Adding htmlhelp to project

I'm trying, without much luck, to add chm options to an existing project.
Main.cpp has this:
#include "HtmlHelp.h"
int OpenHelp(LPTSTR arg1)
{
HWND _Hhdl = HtmlHelp(GetDesktopWindow(), arg1, HH_DISPLAY_TOPIC, NULL);
return 1;
}
htmlhelp.lib is set in Linker/Input/additional Dependencies.
The error I'm getting is:
main.obj : error LNK2019: unresolved external symbol _HtmlHelpW#16 referenced in function "int __cdecl OpenHelp(wchar_t *)" (?OpenHelp##YAHPA_W#Z)
I'm new to C++ so I'm assuming my function is at fault somehow.
The idea was the function would be passed a sting in the "C:\Help.chm\::/Topic.html" format.

C++ Program to connect with MySQL

I am new to C++ (although I have some experience with C) and MySQL and I am trying to make a program that reads a database from MySQL, I have been following this tutorial but I get an error when I try to 'build' the solution. (I am using Visual C++ 2008 just like they do in the tutorial.
Compiling...
test2.cpp
c:\users\rafael\documents\visual studio 2008\projects\test2\test2\test2.cpp(43) : warning C4244: 'initializing' : conversion from 'my_ulonglong' to 'unsigned int', possible loss of data
Compiling manifest to resources...
Microsoft (R) Windows (R) Resource Compiler Version 6.0.5724.0
Copyright (C) Microsoft Corporation. All rights reserved.
Linking...
test2.obj : error LNK2019: unresolved external symbol _mysql_close#4 referenced in function _main
test2.obj : error LNK2019: unresolved external symbol _mysql_fetch_row#4 referenced in function _main
test2.obj : error LNK2019: unresolved external symbol _mysql_num_rows#4 referenced in function _main
test2.obj : error LNK2019: unresolved external symbol _mysql_store_result#4 referenced in function _main
test2.obj : error LNK2019: unresolved external symbol _mysql_query#8 referenced in function _main
test2.obj : error LNK2019: unresolved external symbol _mysql_real_connect#32 referenced in function _main
test2.obj : error LNK2019: unresolved external symbol _mysql_init#4 referenced in function _main
C:\Users\*****\Documents\Visual Studio 2008\Projects\test2\Debug\test2.exe : fatal error LNK1120: 7 unresolved externals
I followed the tutorial and I cannot figure out what's going on, I guess it's something to do with the linkers, but I do not know what could I do.
This is the code I am using (source):
#include "stdafx.h"
#include "my_global.h" // Include this file first to avoid problems
#include "mysql.h" // MySQL Include File
#define SERVER "localhost"
#define USER "root"
#define PASSWORD "********"
#define DATABASE "test"
int main()
{
MYSQL *connect; // Create a pointer to the MySQL instance
connect=mysql_init(NULL); // Initialise the instance
/* This If is irrelevant and you don't need to show it. I kept it in for Fault Testing.*/
if(!connect) /* If instance didn't initialize say so and exit with fault.*/
{
fprintf(stderr,"MySQL Initialization Failed");
return 1;
}
/* Now we will actually connect to the specific database.*/
connect=mysql_real_connect(connect,SERVER,USER,PASSWORD,DATABASE,0,NULL,0);
/* Following if statements are unneeded too, but it's worth it to show on your
first app, so that if your database is empty or the query didn't return anything it
will at least let you know that the connection to the mysql server was established. */
if(connect){
printf("Connection Succeeded\n");
}
else{
printf("Connection Failed!\n");
}
MYSQL_RES *res_set; /* Create a pointer to recieve the return value.*/
MYSQL_ROW row; /* Assign variable for rows. */
mysql_query(connect,"SELECT * FROM TABLE");
/* Send a query to the database. */
unsigned int i = 0; /* Create a counter for the rows */
res_set = mysql_store_result(connect); /* Receive the result and store it in res_set */
unsigned int numrows = mysql_num_rows(res_set); /* Create the count to print all rows */
/* This while is to print all rows and not just the first row found, */
while ((row = mysql_fetch_row(res_set)) != NULL){
printf("%s\n",row[i] != NULL ?
row[i] : "NULL"); /* Print the row data */
}
mysql_close(connect); /* Close and shutdown */
return 0;
}
Yes, it totally is a linker problem: it is almost surely missing the mysql library. In the project settings you should add the name (and the path) of the .lib you intend to use.

Linker error when installing MessagePack implementation for C and C++

I'm following the instructions, which tell me to download msgpack 0.5.4 for C & C++.
On Windows, download source package from here and extract it. Open msgpack_vc8.vcproj or msgpack_vc2008 file and build it using batch build. It builds libraries in lib/ folder and header files in include/ folder.
You can build using command line as follows:
vcbuild msgpack_vc2008.vcproj
dir lib % DLL files are here
dir include % header files are here
vcbuild msgpack_vc2008.vcproj has been replaced by MSBuild msgpack_vc8.vcxproj. I used Visual studio 2012 to convert the project to have the correct .vcxproj for this. Batch build in Visual studio and running MSBuild gives the same result, so I will speak for both of them from this point on.
After the project is converted, I noticed the project was set to output to .lib, rather than .dll, so I altered that setting to match my needs. When compiling there was one small error:
...\microsoft visual studio 11.0\vc\include\stdint.h(8): error C2371: 'int8_t' : redefinition; different basic types
...msgpack-0.5.4\src\msgpack\sysdep.h(23) : see declaration of 'int8_t'
So I changed the line
typedef __int8 int8_t;
to
typedef signed __int8 int8_t;
which solves that minor issue. But then we arrive at where I'm at now. This linker error:
objectc.obj : error LNK2019: unresolved external symbol __imp__ntohl#4 referenced in function _msgpack_pack_array
unpack.obj : error LNK2001: unresolved external symbol __imp__ntohl#4
objectc.obj : error LNK2019: unresolved external symbol __imp__ntohs#4 referenced in function _msgpack_pack_array
unpack.obj : error LNK2001: unresolved external symbol __imp__ntohs#4
...\msgpack-0.5.4\Debug\MessagePack.dll : fatal error LNK1120: 2 unresolved externals
I've searched for parts of this error:
In sysdep.h:
#define _msgpack_be16(x) ntohs(x)
#define _msgpack_be32(x) ntohl(x)
In object.c:
case MSGPACK_OBJECT_ARRAY:
{
int ret = msgpack_pack_array(pk, d.via.array.size);
if(ret < 0) { return ret; }
msgpack_object* o = d.via.array.ptr;
msgpack_object* const oend = d.via.array.ptr + d.via.array.size;
for(; o != oend; ++o) {
ret = msgpack_pack_object(pk, *o);
if(ret < 0) { return ret; }
}
In unpack.c:
static inline int template_callback_array(unpack_user* u, unsigned int n, msgpack_object* o)
{
o->type = MSGPACK_OBJECT_ARRAY;
o->via.array.size = 0;
o->via.array.ptr = (msgpack_object*)msgpack_zone_malloc(u->z, n*sizeof(msgpack_object));
if(o->via.array.ptr == NULL) { return -1; }
return 0;
}
And that's about all I know. If there's another way of how to obtain the .dll, that would be helpful too. Thank you in advance. :)
You need to link the ws2_32.lib library since ntohl is a winsocket API function.
That should fix the problem!