MFC project has linking error on Visual Studio 2013? - visual-studio-2013

When I tried to add a toolbar to my mfc project in Visual studio 2013, the following errors occurred. Has anyone worked on this? How can I fix the errors?
mfc project in Visual studio 2013
MFCModalOynaDlg.h
#pragma once
class CMFCModalOynaDlg : public CDialogEx
{
public:
CMFCModalOynaDlg(CWnd* pParent = NULL);
enum { IDD = IDD_MFCMODALOYNA_DIALOG };
protected:
virtual void DoDataExchange(CDataExchange* pDX);
protected:
HICON m_hIcon;
CToolBar m_wndToolBar;
BOOL butD;
virtual BOOL OnInitDialog();
afx_msg void OnPaint();
afx_msg HCURSOR OnQueryDragIcon();
DECLARE_MESSAGE_MAP()
public:
afx_msg void OnBnClickedOk();
};
MFCModalOynaDlg.cpp:
#include "stdafx.h"
#include "MFCModalOyna.h"
#include "MFCModalOynaDlg.h"
#include "afxdialogex.h"
#include "windows.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
CMFCModalOynaDlg::CMFCModalOynaDlg(CWnd* pParent /*=NULL*/)
: CDialogEx(CMFCModalOynaDlg::IDD, pParent)
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MYICON1);
}
void CMFCModalOynaDlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(CMFCModalOynaDlg, CDialogEx)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
END_MESSAGE_MAP()
BOOL CMFCModalOynaDlg::OnInitDialog()
{
CDialogEx::OnInitDialog();
SetIcon(m_hIcon, TRUE);
SetIcon(m_hIcon, FALSE);
if (!m_wndToolBar.Create(this)|| !m_wndToolBar.LoadToolBar(IDR_TOOLBAR1))
{
TRACE0("Failed to Create Dialog Toolbar\n");
EndDialog(IDCANCEL);
}
butD = TRUE;
CRect rcClientOld;
CRect rcClientNew;
GetClientRect(rcClientOld);
RepositionBars(AFX_IDW_CONTROLBAR_FIRST, AFX_IDW_CONTROLBAR_LAST, 0, reposQuery, rcClientNew);
CPoint ptOffset(rcClientNew.left - rcClientOld.left, rcClientNew.top - rcClientOld.top);
CRect rcChild;
CWnd* pwndChild = GetWindow(GW_CHILD);
while (pwndChild)
{
pwndChild->GetWindowRect(rcChild);
ScreenToClient(rcChild);
rcChild.OffsetRect(ptOffset);
pwndChild->MoveWindow(rcChild, FALSE);
pwndChild = pwndChild->GetNextWindow();
}
CRect rcWindow;
GetWindowRect(rcWindow);
rcWindow.right += rcClientOld.Width() - rcClientNew.Width();
rcWindow.bottom += rcClientOld.Height() - rcClientNew.Height();
MoveWindow(rcWindow, FALSE);
RepositionBars(AFX_IDW_CONTROLBAR_FIRST, AFX_IDW_CONTROLBAR_LAST, 0);
return TRUE;
}
Error 1 error LNK2001: unresolved external symbol "protected: void
__thiscall CMFCModalOynaDlg::OnPaint(void)" (?OnPaint#CMFCModalOynaDlg##IAEXXZ)
Error 2 error LNK2001: unresolved external symbol "protected: struct HICON__ * __thiscall CMFCModalOynaDlg::OnQueryDragIcon(void)" (?OnQueryDragIcon#CMFCModalOynaDlg##IAEPAUHICON__##XZ)
Error 3 error LNK1120: 2 unresolved externals D:\VSprogram\MFCModalOyna\Debug\MFCModalOyna.exe
When I tried to add a toolbar to my mfc project in Visual studio 2013, the following errors occurred.

Related

OMNET++. Using custom message from imported dynamic library

I have two omnetpp projects: Lab1 and Lab2
From Lab1 .so library is created. Lab 2 is supposed to use this library. It is clear how to import and use modules declared in .ned-files. Problems begin when I try to use message-type from the library (Bitmessage).
bitmessage.msg from the library:
message Bitmessage {
bool bit;
}
sSource.h in Lab2:
#ifndef SSOURCE_H_
#define SSOURCE_H_
#include <omnetpp.h>
#include "bitmessage_m.h"
class sSource : public cSimpleModule
{
protected:
virtual void initialize();
virtual void handleMessage(cMessage *msg);
private:
double p;
};
Define_Module(sSource);
#endif /* SSOURCE_H_ */
sSource.cc - source-file in Lab2 which is suppose to use bitmessage:
#include <sSource.h>
/*virtual*/ void sSource::initialize()
{
p = par("p").doubleValue();
}
/*virtual*/ void sSource::handleMessage(cMessage *msg)
{
if (msg->getArrivalGate() == gate("in"))
{
delete msg;
Bitmessage* bit_msg = new Bitmessage(); // **(!!!)here is error**
int num_outs = this->gateSize("out");
for (int i = 0; i < num_outs; ++i)
{
send(bit_msg->dup(), "out", i);
}
delete bit_msg;
}
else
{
delete msg;
}
}
The error is following:
undefined reference to `Bitmessage::Bitmessage(char const*, int)'
How to deal with it? Maybe the problem is that compiler can't interpret .msg-file from the library?

CListCtrl resets horizontal scroll position

I'm having an issue with an MFC CListCtrl in report mode where whenever
the horizontal scroll position is not at the beginning
and the Shift key is pressed
as soon as an item is selected via a left-button mouse click, the horizontal scroll state reverts back to the beginning.
I'm using Visual Studio 2015 and was able to reproduce this with an example application. A simple dialog-based MFC application with a CListCtrl, two columns and two items shows this behavior under Windows 7 and Windows 10.
Is there a way to prevent this, so that the CListCtrl keeps the scroll position?
Update
I've added a MCVE below, where I used the New-Project Wizard of VS 2015 to create a dialog-based MFC application named ListScroll where I statically link MFC.
I've also noticed that there doesn't need to be already one item selected. Even selecting one item with the Shift key pressed shows this on my systems.
ListScroll.h
#pragma once
#ifndef __AFXWIN_H__
#error "include 'stdafx.h' before including this file for PCH"
#endif
#include "resource.h"
class CListScrollApp : public CWinApp
{
public:
CListScrollApp();
public:
virtual BOOL InitInstance();
virtual int ExitInstance();
DECLARE_MESSAGE_MAP()
};
extern CListScrollApp theApp;
ListScroll.cpp
#include "stdafx.h"
#include "ListScroll.h"
#include "ListScrollDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
BEGIN_MESSAGE_MAP(CListScrollApp, CWinApp)
END_MESSAGE_MAP()
CListScrollApp::CListScrollApp() {}
CListScrollApp theApp;
BOOL CListScrollApp::InitInstance() {
CWinApp::InitInstance();
CListScrollDlg dlg;
m_pMainWnd = &dlg;
INT_PTR nResponse = dlg.DoModal();
if (nResponse == IDOK) {
// Do nothing
} else if (nResponse == IDCANCEL) {
// Do nothing
} else if (nResponse == -1) {
TRACE(traceAppMsg, 0, "Warning: dialog creation failed, so application is terminating unexpectedly.\n");
}
return FALSE;
}
int CListScrollApp::ExitInstance() {
return CWinApp::ExitInstance();
}
ListScrollDlg.h
#pragma once
#include "afxcmn.h"
class CListScrollDlg : public CDialog
{
public:
CListScrollDlg(CWnd* pParent = NULL);
#ifdef AFX_DESIGN_TIME
enum { IDD = IDD_LISTSCROLL_DIALOG };
#endif
protected:
virtual void DoDataExchange(CDataExchange* pDX);
virtual BOOL OnInitDialog();
DECLARE_MESSAGE_MAP()
private:
CListCtrl m_wndList;
};
ListScrollDlg.cpp
#include "stdafx.h"
#include "ListScroll.h"
#include "ListScrollDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
CListScrollDlg::CListScrollDlg(CWnd* pParent /*=NULL*/)
: CDialog(IDD_LISTSCROLL_DIALOG, pParent)
{}
void CListScrollDlg::DoDataExchange(CDataExchange* pDX) {
CDialog::DoDataExchange(pDX);
DDX_Control(pDX, IDC_LIST1, m_wndList);
}
BEGIN_MESSAGE_MAP(CListScrollDlg, CDialog)
END_MESSAGE_MAP()
BOOL CListScrollDlg::OnInitDialog() {
CDialog::OnInitDialog();
m_wndList.InsertColumn(1, _T("Test 1"), 0, 200);
m_wndList.InsertColumn(1, _T("Test 2"), 0, 800);
m_wndList.InsertItem(1, _T("Item 1"), 0);
m_wndList.InsertItem(1, _T("Item 2"), 0);
return TRUE;
}
ListScroll.rc
IDD_LISTSCROLL_DIALOG DIALOGEX 0, 0, 320, 200
STYLE DS_SETFONT | DS_FIXEDSYS | WS_MINIMIZEBOX | WS_MAXIMIZEBOX | WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_SYSMENU | WS_THICKFRAME
EXSTYLE WS_EX_APPWINDOW
CAPTION "ListScroll"
FONT 8, "MS Shell Dlg", 0, 0, 0x1
BEGIN
DEFPUSHBUTTON "OK",IDOK,209,179,50,14
PUSHBUTTON "Cancel",IDCANCEL,263,179,50,14
CTEXT "TODO: Place dialog controls here.",IDC_STATIC,10,96,300,8
CONTROL "",IDC_LIST1,"SysListView32",LVS_REPORT | LVS_ALIGNLEFT | WS_BORDER | WS_TABSTOP,7,7,306,162
END

error calling vc++ method error LNK2019: unresolved external symbol referenced in function _wmain

shows error calling vc++ 2010 express sp1 method of a object:
error LNK2019: unresolved external symbol "public: int __thiscall
oper::ShowMyResult(void)" (?ShowMyResult#oper##QAEHXZ) referenced in
function _wmain
ooad_oops_exer.h
#include "stdafx.h"
class oper {
public:
void DisplayDefault( );
void DisplayWidth( int n );
void DisplayLongs( );
int ShowMyResult();
};
ooad_oops_exer.cpp
#include "ooad_oops_exer.h"
//
#ifndef SOCKET_CLIENT_CLASS
#define SOCKET_CLIENT_CLASS
const double d1 = 1.23456789;
const long l1 = 16;
int base = 10;
void oper::DisplayDefault( )
{
cout << endl << "default display" << endl;
}
void oper::DisplayWidth( int n )
{
cout << endl << "fixed width display set to " << n << ".\n";
}
void oper::DisplayLongs( )
{
cout <<endl<<"hello";
}
int oper::ShowMyResult()
{
DisplayWidth(15);
DisplayDefault( );
cout <<endl<<"ShowMyResult";
return 0;
}
//
#endif
main.cpp
#include "stdafx.h"
#include "ooad_oops_exer.h"
int _tmain(int argc, _TCHAR* argv[])
{
oper op;
int k = op.ShowMyResult();
cin.get();
return 0;
}
EDIT
1> Generating Code... 1>Debug\ooad_oops_exer.obj : warning LNK4042:
object specified more than once; extras ignored 1>main.obj : error
LNK2019: unresolved external symbol "public: int __thiscall
oper::ShowMyResult(void)" (?ShowMyResult#oper##QAEHXZ) referenced in
function _wmain 1>C:\xxx\OOAD_OOPS_PROJ\Debug\OOAD_OOPS_PROJ.exe :
fatal error LNK1120: 1 unresolved externals 1>

stack select() function doesn't work in Visual Studio

The following is part of the example code from http://curl.haxx.se/libcurl/c/sendrecv.html:
static int wait_on_socket(curl_socket_t sockfd, int for_recv, long timeout_ms)
{
struct timeval tv;
fd_set infd, outfd, errfd;
int res;
tv.tv_sec = timeout_ms / 1000;
tv.tv_usec= (timeout_ms % 1000) * 1000;
FD_ZERO(&infd);
FD_ZERO(&outfd);
FD_ZERO(&errfd);
FD_SET(sockfd, &errfd); /* always check for error */
if(for_recv)
{
FD_SET(sockfd, &infd);
}
else
{
FD_SET(sockfd, &outfd);
}
///* select() returns the number of signalled sockets or -1 */
res = select(sockfd + 1, &infd, &outfd, &errfd, &tv);
return res;
}
It worked fine on Linux, but when I brought it over Visual Studio 2010, it complains about the select() function. I compiled and linked all the cURL libraries/dlls correctly, and I even downloaded stuff as http request.
But when I tried to use the socket select() function, it complains with the following error:
error LNK2028: unresolved token (0A00034C) "extern "C" int __stdcall select(int,struct fd_set *,struct fd_set *,struct fd_set *,struct timeval const *)" (?select##$$J220YGHHPAUfd_set##00PBUtimeval###Z) referenced in function "int __cdecl `anonymous namespace'::wait_on_socket(unsigned int,int,long)" (?wait_on_socket#?A0x7d9db21b##$$FYAHIHJ#Z)
error LNK2019: unresolved external symbol "extern "C" int __stdcall select(int,struct fd_set *,struct fd_set *,struct fd_set *,struct timeval const *)" (?select##$$J220YGHHPAUfd_set##00PBUtimeval###Z) referenced in function "int __cdecl `anonymous namespace'::wait_on_socket(unsigned int,int,long)" (?wait_on_socket#?A0x7d9db21b##$$FYAHIHJ#Z)
Also, it seems that the select() function is compatible with Windows:
http://msdn.microsoft.com/en-us/library/ms740141(VS.85).aspx
Am I missing something? Can someone tell me how to get around this problem?
Thanks, --Rudy
You must link with the Microsoft socket library. Tell your linker to use Ws2_32.lib within your project.
In Visual Studio 2010 add this lib to Project->Properties->Linker->Input->Add. Dependencies.
Or add this lib inside your code with:
#pragma comment(lib, "Ws2_32.lib")

Error generating log file in the win32 console project

I have a code to generate a log file :-
// INI.cpp: implementation of the CINI class.
//#include "stdafx.h"
#include "INI.h"
#include <iostream>
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
CINI::CINI(char* szFileName)
{
memset(m_szFileName, 0x00, 255);
memcpy(m_szFileName, szFileName, strlen(szFileName));
}
CINI::~CINI()
{
}
float CINI::ReadFloat(char* szSection, char* szKey, float fltDefaultValue)
{
char szResult[255];
char szDefault[255];
float fltResult; sprintf(szDefault, "%f",fltDefaultValue);
GetPrivateProfileString(szSection, szKey, szDefault, szResult, 255, m_szFileName);
fltResult = atof(szResult); return fltResult;
}
bool CINI::ReadBoolean(char* szSection, char* szKey, bool bolDefaultValue)
{
char szResult[255];
char szDefault[255];
bool bolResult;
sprintf(szDefault, "%s", bolDefaultValue? "True" : "False");
GetPrivateProfileString(szSection, szKey, szDefault, szResult, 255, m_szFileName);
bolResult = (strcmp(szResult, "True") == 0 || strcmp(szResult, "true") == 0) ? true : false; return bolResult;
}
char* CINI::ReadString(char* szSection, char* szKey, const char* szDefaultValue)
{
memset(m_szResult, 0x00, 255);
GetPrivateProfileString(szSection, szKey, szDefaultValue, m_szResult, 255, m_szFileName);
return m_szResult;
}
void CINI::WriteInteger(char* szSection, char* szKey, int iValue)
{
char szValue[255]; sprintf(szValue, "%d", iValue);
WritePrivateProfileString(szSection, szKey, szValue, m_szFileName);
}
void CINI::WriteFloat(char* szSection, char* szKey, float fltValue)
{
char szValue[255];
sprintf(szValue, "%f", fltValue);
WritePrivateProfileString(szSection, szKey, szValue, m_szFileName);
}
void CINI::WriteBoolean(char* szSection, char* szKey, bool bolValue)
{
char szValue[255]; sprintf(szValue, "%s", bolValue ? "True" : "False");
WritePrivateProfileString(szSection, szKey, szValue, m_szFileName);
}
void CINI::WriteString(char* szSection, char* szKey, char* szValue)
{
WritePrivateProfileString(szSection, szKey, szValue, m_szFileName);
}
I am getting the following errors when I run the code:-
c:\program files\microsoft visual studio 10.0\vc\atlmfc\include\afx.h(24): fatal error C1189: #error : Building MFC application with /MD[d] (CRT dll version) requires MFC shared dll version. Please #define _AFXDLL or do not use /MD[d]
When I include the StdAfx.h and StdAfx.cpp i get the following errors:-
StdAfx.cpp
1>c:\program files\microsoft visual studio 10.0\vc\atlmfc\include\afx.h(24): fatal error C1189: #error : Building MFC application with /MD[d] (CRT dll version) requires MFC shared dll version. Please #define _AFXDLL or do not use /MD[d]
1> INI.cpp
1>c:\program files\microsoft visual studio 10.0\vc\atlmfc\include\afx.h(24): fatal error C1189: #error : Building MFC application with /MD[d] (CRT dll version) requires MFC shared dll version. Please #define _AFXDLL or do not use /MD[d]
I am using VS2010 and need to build win32 console project. Please help.
You have to change your project's properties:
Go to the Project-> Properties (the last menu item) -> Configuration Properties -> General.
There you'll see the "Project Defaults" section with the "Use of MFC" option. Choose the "Use MFC in a static library".
Basically, you're getting the conflict, because in the code generation settings the multi-threaded static run-time library is chosen and the MFC library that you're trying to use is built using the dll-based C run-time.

Resources