PDCurses TUI C++ Win32 console app - Access violation reading location - c++

I have downloaded pdcurses source and was able to successfully include curses.h in my project, linked the pre-compiled library and all good.
After few hours of trying out the library, I saw the tuidemo.c in the demos folder, compiled it into an executable and brilliant! exactly what I needed for my project.
Now the problem is that it's a C code, and I am working on a C++ project in VS c++ 2008.
The files I need are tui.c and tui.h
How can I include that C file in my C++ code? I saw few suggestions here
but the compiler was not too happy with 100's of warnings and errors.
How can I go on including/using that TUI pdcurses includes!?
Thanks
EDIT:
I added extern "C" statement, so my test looks like this now, but I'm getting some other type of error
#include <stdio.h>
#include <stdlib.h>
using namespace std;
extern "C" {
#include <tui.h>
}
void sub0()
{
//do nothing
}
void sub1()
{
//do nothing
}
int main (int argc, char * const argv[]) {
menu MainMenu[] =
{
{ "Asub", sub0, "Go inside first submenu" },
{ "Bsub", sub1, "Go inside second submenu" },
{ "", (FUNC)0, "" } /* always add this as the last item! */
};
startmenu(MainMenu, "TUI - 'textual user interface' demonstration program");
return 0;
}
Although it is compiling successfully, it is throwing an Error at runtime, which suggests a bad pointer:
0xC0000005: Access violation reading location 0x021c52f9
at line
startmenu(MainMenu, "TUI - 'textual user interface' demonstration program");
Not sure where to go from here.
thanks again.

If I'm not mistaken (and I could easily be), it's due to the difference in calling conventions for C/C++. Try making the callbacks extern "C", and make them call a C++ function. Call it a trampoline :)

Finally got it working. The solution was in the steps below:
First I renamed tui.c to tui.cpp
For the header tui.h, I followed the exact same step of wrapping the code as described here.
then in my project i just included the header without any extern "C" block
#include "tui.h"
Compiled and it worked!

Related

Using C Functions in C++ Using extern "C"

Iam trying to access two of my C functions in a Cpp code using extern "C". I am using it as follows :
#include <stdio.h>
#include <stdlib.h>
#include <cstdio>
extern "C"
{
#include "pltfrm.h"
#include "xil_printf.h"
}
int main()
{
init_platform();
print("Hello World\n\r");
print("Successfully ran Hello World application");
cleanup_platform();
return 0;
}
Functions "init_platform" and "cleanup_platform" are declared inside "pltfrm.h"
Iam getting "Undefined reference to" error on both functions.What am i doing wrong? iam compiling using eclipse build project option for c++ empty project template.
That message comes from the linking step, when all source files are compiled and the linker tries to connect all functions to the function calls.
You probably need to link to some library or add the source files, for the c-headers you added, to your project.
#Gotiasits Answer was helpful in this case.I removed extern C and it seems native support is there. Will run the executbale in target and check

How can I create a c++ header file in Visual Studio Code?

I have a program with 3 files, and when IrRun the program via Code Runner it keeps printing errors.The icon for the "Log.hpp" file is C, not C++. It doesnt matter if I rename it to Log.h or anything, it seems that I cant create a c++ header file in vscode.
The 3 files are:
main.cpp
#include <iostream>
#include "log.hpp"
using namespace std;
int main() {
InitLog();
Log("Hello World");
return 0;
}
log.cpp
#include "log.hpp"
#include <iostream>
void InitLog() {
Log("Initializing Log");
}
void Log(const char *message) {
std::cout << message << std::endl;
}
log.hpp
#pragma once
void InitLog();
void Log(const char *message);
The error mesages are:
main.cpp: In function 'int main()':
main.cpp:5:5: error: 'InitLog' was not declared in this scope
InitLog();
^~~~~~~
main.cpp:6:5: error: 'Log' was not declared in this scope
Log("Hello World");
Need help.
Are you using Windows? Which compiler? Do you have Microsoft Build Tools installed or even Visual Studio? Are you using gcc or clang?
You must start VS Code from a developer prompt: Open a developer prompt console, navigate to the folder where you code is. Then enter
code .
Visual Studo Code will then open with the environment set up. Then open a terminal inside VSCode using Control-` and try
cl /EHsc main.cpp log.cpp
And it will create main.exe. You can run it in the terminal and it will not close... Next time you open the project you can just open the folder in VS Code, since it would then already have the json config files created
VS Code is just an IDE. So compilers must be installed and also extensions. And some JSON files and tasks must be set up. It is sometimes simple, sometimes not so simple, I believe. But doing that you will then have a powerful editor and an unbelievably flexible environment, since you can for instance run and debug code in nodejs or C or C++ or anything and even in Linux without leaving the session on your casual Windows machine.
Your question is about a specific vscode plugin called Code Runner. It's not really related to vscode. The way how Code Runner is designed will never work for multiple C++ source files. It's not the right tool for the job.
Perhaps you'd better off with Microsofts CMake Tools package for vscode. It does require you to create your own CMakeLists.txt file though. In your case you'd need nothing more than:
add_executable(log
main.cpp
log.cpp
)

C++ Stack cookie buffer overrun on UE4 with DLL

I'm trying to include a dll into unreal engine 4. So until now, I'm just trying to print a string coming from the DLL. But when i try to use the function of the DLL i immediatly got this error:
Exception thrown at 0x00007FFACC964C80 in (UE4Editor-Prototype.dll) in UE4Editor.exe: stack cookie instrumentation code detected a stack-based buffer overrun
I've check that the issue really come from the function call, if i remove it, I don't get the error.
I've try to put it in a char, string or even nothing but it didn't do anything.
I've tried to catch the error but it never succeed, unreal just crash. I succeed to get the error with visual studio debugger. When i go step by step. i can see, the DLL is found but after BeginPlay, it crash.
Here is the code in unreal which is not working:
void UMyTestComponent::BeginPlay()
{
Super::BeginPlay();
//It's just some test code, i know it's executed once, this code appears
//inside the log
UE_LOG(LogTemp, Warning, TEXT("TEEEEEEEEEEEEEEEEEST1"));
try {
//Call to the DLL, this call make UE4 crash
Test_init();
}
catch (std::exception e) {
//Nothing is ever written here
UE_LOG(LogTemp, Warning, TEXT("%s"), e.what());
}
}
here is my Dll file .h
#pragma once
#include <string>
#define PARSER_API __declspec(dllexport)
extern PARSER_API std::string Test_init();
and here is my Dll file .cpp
#include "pch.h"
#include <utility>
#include <limits.h>
#include <iostream>
#include "Test.h"
std::string Test_init()
{
return std::string("DLLLLLLLL");
}
I'm working on windows 10, UE 4.22.3 and visual studio 2019
The goal would be to catch the string and to print it in an :
UE_LOG() to print it.
So i will print "DLLLLL" just to check the correct linkage
Edit:
I've tried several formatting with __declspec(dllexport). but none is working, the C linkage too.

fstream constructor causing access violation

I'm having trouble reading from a file. I've cut my program down and down until all I'm left with is not much more than Hello World - and I'm still getting an error of 'access violation'.
The error is occuring on the line fstream f(..); - it never gets as far as f.good(). The only changes to this project have been to include ozz-animation header files and .lib's (easily found on Google), as well as setting Runtime Library mode to /MT rather than /MD. Ozz was throwing the same error when attempting to read a file (hence the cutting down until I'm left with what you see). The file is present, although I don't believe it should matter. Code is as follows:
#pragma once
#include <Windows.h>
#include <fstream>
using namespace std;
int main()
{
fstream f("C:\\Users\\thoma_000\\Desktop\\Limitless\\Bin\\Asset\\skin_example\\A.txt");
if (f.good())
{
OutputDebugString(L"\n\n\nGOOD.\n\n\n");
}
else
{
OutputDebugString(L"\n\n\nBAD.\n\n\n");
}
return 0;
}
I managed to resolve this by creating the project initially as an empty win32 project rather than a Windows project. I have been through the project settings and I currently can't tell what setting is causing the problem - but at least I have a resolution.

VC++ 2010 - Undeclared Identifier in attempt at DLL, small amount of code

C++ newbie here. I'm trying to put some WIA functions in a DLL. I keep getting and undeclared identifier on the IWiaDevMgr variable. When creating the project I chose the Win32 Console Application and DLL application type. Not sure if it matters but I put the wiaguid.lib in the project
properties -> Linker -> input -> additional dependencies.
What is wrong with this code?
MyDLL.h
#include <wia.h>
namespace MyDLL
{
class MyFirstFuncs
{
public:
static __declspec(dllexport) int doWork();
};
}
MyDLL.cpp
#include "MyDLL.h"
namespace MyDLL
{
int MyFirstFuncs::doWork()
{
IWiaDevMgr *pIWiaDevMgr;
}
}
I had the exact same problem. Through trial and error I found that
#include <windows.h>
#include <wia.h>
fixed the problem.
I'm a C++ newbie also so couldn't tell you the exact reason why this works. Probably WIA is dependant on some definitions/macros/whatever in WINDOWS.H
Check the order in which you have included your header files. It may be the same problem like the one I had in programming a Directshow application. I had included vmr9.h before d3d9.h. During the build process, the compiler fired errors concerning d3d9 objects included in the vmr9.h. I had to reorder the inclusions to solve the problem