Debugging file scope in Visual Studio - debugging

In Visual Studio 2012, I have this uber-simple C++ program:
#include "stdafx.h"
#include <iostream>
int _tmain(int argc, _TCHAR* argv[])
{
std::cout << std::cout;
int i = 1;
return 0;
}
In case you wonder, the "std::cout << std::cout" part is only to show that std:cout can be accessed within the scope of the function - yes, it's meant to print the pointer to the console.
So I set a breakpoint at "int i = 1". When the breakpoint triggers, I want to inspect std::cout from the Command Window (or Immediate Window), so I type:
>Debug.Print std::cout
But it returns the following error:
identifier "std" is undefined
I don't understand why this happens, std should be in the scope of the function which is being executed. The same goes for any other stuff coming from the #include directive, I just can't inspect it using Debug.Print. What do I use in Visual Studio 2012 to inspect EVERYTHING accessible in the execution scope? It seems I can only access local variables with Debug.Print.

Related

keyboard interrupt routine visual studio C++ console app

I am using VS 2022 Preview to write a C++ console application. I wish to detect a keyboard hit and have my interrupt handler function called. I want the key press detected quickly in case main is in a long loop and therefore not using kbhit().
I found signal() but the debugger stops when the Control-C is detected. Maybe it is a peculiarity of the IDE. Is there a function or system call that I should use?
Edit: I am vaguely aware of threads. Could I spawn a thread that just watches kbd and then have it raise(?) an interrupt when a key is pressed?
I was able to do it by adding a thread. On the target I will have real interrupts to trigger my ISR but this is close enough for algorithm development. It seemed that terminating the thread was more trouble than it was worth so I rationalized that I am simulating an embedded system that does not need fancy shutdowns.
I decided to just accept one character at a time in the phony ISR then I can buffer them and wait and process the whole string when I see a CR, a simple minded command line processor.
// Scheduler.cpp : This file contains the 'main' function. Program execution begins and ends there.
//
#include <Windows.h>
#include <iostream>
#include <thread>
#include <conio.h>
void phonyISR(int tbd)
{
char c;
while (1)
{
std::cout << "\nphonyISR() waiting for kbd input:";
c = _getch();
std::cout << "\nGot >" << c << "<";
}
}
int main(int argc, char* argv[])
{
int tbd;
std::thread t = std::thread(phonyISR, tbd);
// Main thread doing its stuff
int i = 0;
while (1)
{
Sleep(2000);
std::cout << "\nMain: " << i++;
}
return 0;
}

libxl library use in c++

When I create a project visual studio 2015 I can work this libxl library hovewer I could not be able to work that library on visual studio qt gui application project.
I try everything whatever I know.
#include "stdafx.h"
#include "QtGuiApplication5.h"
#include <QtWidgets/QApplication>
#include <qapplication.h>
#include <qpushbutton.h>
#include <iostream>
#include <conio.h>
#include "libxl.h"
using namespacenclude <Qt libxl;
using namespace std;
int main(int argc, char *argv[])
{
Book* book = xlCreateBook();
if (book)
{
if (book->load(L"..\\Lab_Bus Datebase.xlsx"))
{
Sheet* sheet = book->getSheet(0);
if (sheet)
{
const wchar_t* s = sheet->readStr(2, 1);
if (s) std::wcout << s << std::endl << std::endl;
}
}
else
{
std::cout << "At first run generate !" << std::endl;
}
book->release();
}
std::cout << "\nPress any key to exit...";
_getch();
QApplication a(argc, argv);
QtGuiApplication5 w;
w.show();
return a.exec();
}
Link2019 error: Severity Code Description Project File Line Suppression State
Error LNK2019 unresolved external symbol __imp_xlCreateBookW referenced in function main QtGuiApplication5
link1120 error: Severity Code Description Project File Line Suppression State
Error LNK1120 1 unresolved externals QtGuiApplication5 C
You need to configure visual studio project properties to use required lib. Refer this link for the same.
You are using .xlsx file so instead of xlCreateBook use xlCreateXMLBook. Apart from this you need to use using namespace libxl;as well
Below are Factory functions:
Book* xlCreateBook()
Create a binary book instance for xls format. This function should be called first for receiving a book pointer. This function and other classes are in libxl namespace.
Book* xlCreateXMLBook()
Create a xml book instance for xlsx format. This function should be called first for receiving a book pointer. This function and other classes are in libxl namespace.
See below image above code works fine at my machine.

basic setup of C++/CLI code so that it can be consumed by unmanaged C/++

I am trying to learn C++/CLI, with the plan of writing a DLL which will be consumed by (unmanaged) C code. However, I cannot get the most basic example to build, as is reproducible below:
I am working in Visual Studio Express 2013.
Create new project -> CLR ->class library
LearnCli.h:
extern "C" __declspec(dllexport)
int __stdcall TestFunc();
LearnCli.cpp:
#include "stdafx.h"
#include "LearnCli.h"
int __stdcall TestFunc()
{
return 3;
}
Build with no problems.
Add Project -> Win32 ->Console Application
From the context menu in solution explorer for the new console project:
Add -> reference -> LearnCli
stdafx.h
#pragma once
#include "targetver.h"
#include <stdio.h>
#include <tchar.h>
// TODO: reference additional headers your program requires here
#include "..\LearnCli\LearnCli.h"
ConsoleApplication.cpp
#include "stdafx.h"
#include <string>
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
int z;
z=TestFunc();
cout << "Function returns:" << z << endl;
cin.get();
return 0;
}
intellisense has no problems, but on build:
Error 1 error LNK2019: unresolved external symbol _TestFunc#0 referenced in function _wmain [path]\Projects\LearnCli\ConsoleApplication1\ConsoleApplication1.obj ConsoleApplication1
What am I missing which is not allowing the win32 console app to find the function? Cheers.
Edit
Thanks to the comment and link, I have change the LearnCli.h file to
#ifdef LEARNCLIAPI_EXPORTS
#define LearnCliApi_DECLSPEC __declspec(dllexport)
#else
#define LearnCliApi_DECLSPEC __declspec(dllimport)
#endif
And gone to Project -> Properties -> C/C++ -> Preprocessor ->Definitions
and added LEARNCLIAPI_EXPORTS. unfortuately the error is unchanged
You need to link your application(exe) project with the .lib built from dll project.
You can add that from Project settings >> Linker >> Input files or simply put a line on your source.
i.e.
pragma(comment, "lib:<your_lib.lib>")

How do I handle errors in Lua when executing arbitrary strings?

I'm going for absolute minimalism here. (It's been a while since I've worked with the Lua C API.)
#include <lua.hpp>
#include <iostream>
#include <string>
using namespace std;
int main(int argc, char** argv)
{
lua_State* state = luaL_newstate();
luaL_openlibs(state);
string input;
while (getline(cin, input))
{
auto error = luaL_dostring(state, input.c_str());
if (error)
{
cerr << "Lua Error: " << lua_tostring(state, -1) << '\n';
lua_pop(state, 1);
}
}
lua_close(state);
return 0;
}
This program works fine as long as I feed it perfect Lua. However, if I enter something bad (such as asdf()), the program crashes! Why is it not handling my error gracefully?
I've tried breaking out the calls before. It crashes on the call to lua_pcall itself. I never make it past that line.
The binary download (5.2.1 I believe) has a bug that was corrected in 5.2.3. I rebuilt the library from source, and now my program works fine.

Phantom syntax errors in Visual C++ 2010 Express

I just started learning C++, and I'm getting weird 'phantom' syntax errors in Visual C++ 2010 Express.
There are red lines under seemingly random pieces of code, and when I hover my cursor over them it shows errors that seem to make no sense at all. However, when I hit F5 the program compiles and runs successfully.
It's hard to learn C++ like this because I can't quickly distinguish between real syntax errors and 'fake' ones.
The syntax errors:
http://i.stack.imgur.com/O0UbD.png
The program:
#include <iostream>
#include "conio.h"
#include "windows.h"
class test2
{
public:
int i;
};
class testc
{
public:
test2 hi;
};
int main()
{
testc hello;
hello.hi.i = 23;
std::cout << hello.hi.i << "\n";
system("pause");
}
I reinstalled entire Visual Studio, not just Visual C++. Now it works correctly.

Resources