C++ Stack cookie buffer overrun on UE4 with DLL - c++

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.

Related

Program won't run when there's any function from the GLEW library (C++)

I have reinstalled windows for other reasons, and after setting up MinGWx64 (my portable compiler of choice), I had it working back again, EXCEPT for a little problem; I tried to compile an old project with OpenGL (using GLFW), and it did it with no exceptions nor errors; but then I tried to run the code, and I just couldn't. No errors, no waiting, nothing, just stopped instantly. So I made some tests, and long story short, it was GLEW. Basically, if you invoke any function from GLEW, the program just won't run, no matter the line from where it is called, but will previously compile without any issues (that have to be with the library I mean).
This is a short example of what that looks like:
#include <iostream>
#include <GL/gl.h>
int main() {
std::cout << "works\n";
glfwInit();
return 0;
};
This works, but now when I add a glew function (those some-variables would be values):
#include <iostream>
#include <GL/glew.h> //Included before (correctly)
#include <GL/gl.h>
int main() {
std::cout << "works\n";
glfwInit();
GLFWwindow* window = glfwCreateWindow((someresulution),(someresolution),(sometitle),NULL,NULL);
glfwMakeContextCurrent(window);
if (glewInit()!=GLEW_OK) std::cout << "glew not ok\n"; //HERE
return 0;
};
Again, it compiles just fine, but the program won't do anything (even tho the function call is below the "works" log, but neither it or "glew not ok" will be logged), just exit instantly.
This leads me to think it is something to be with the linking, maybe it not finding glew32.lib, but I have checked all files were in the right dirs like 4 times. The path's ok, vscode settings are ok (if that helps anyway), and I just can't figure it out, as it is the first time something like this happens to me. If there's anyone that knows about a solution or something about it, I would be pleased to have some help, any suggestions will be appreciated. Thanks.
Did you define GLEW_STATIC for static version or GLEW_BUILD for dll version in your program ?? I guess without defining those will create compilation errors in glew

Arduino / esp8266: error: section attribute not allowed for 'Pchr

My Arduino program had compiled and worked correctly but something happen or I did something because now when I compile it, I get an compiler error. I have tried upgrading esp8266 v2.4.1 to 2.5.2 but then I get another error in the random.tcc header file. I am compiling my code with Visual Studio 2017 with Visual Micro. I have seen this error on the web but upgrading didn't seem to help.
Any suggestions?
OUTPUT
Adafruit_ESP8266.cpp:17: In file included from
Adafruit_ESP8266.h: 28:35: error: section attribute not allowed for 'Pchr
typedef const PROGMEM char Pchr; \\ Ditto, kindasorta
SOURCE
#include <Adafruit_ESP8266.h>
#include <ESP8266WiFi.h> //https://github.com/esp8266/Arduino
//needed for library
#include <DNSServer.h>
#include <ESP8266WebServer.h>
#include <WiFiManager.h> //https://github.com/tzapu/WiFiManager
// the setup function runs once when you press reset or power the board
void setup()
{
Serial.println("Begin Setup");
Serial.println("End Setup");
}
// the loop function runs over and over again until power down or reset
void loop()
{
Serial.println("Begin Loop");
Serial.println("End Loop");
}
I fixed my error by removing the header file Adafruit_ESP8266.h. I must of clicked on something that included it.

Calling C DLL from C++ gives Access Violation but C# project with DllImport working

I have a 32-bit 3rd party C DLL that I need to integrate with, but am hitting an error that's difficult to understand/track down.
I can compile and successfully link a simple C++ 32-bit app consisting of the following:
#include "stdafx.h"
#include <windows.h>
extern "C" int __stdcall external_method(int cardno);
int main()
{
int n = external_method(0);
return 0;
}
When I try to run the app though in debug mode it gives the following:
Unhandled exception at 0x100AADF5 (ExternalModule.dll) in
ConsoleApplication2.exe: 0xC0000005: Access violation reading location 0x00000000.
However, if I create a simple C# application that uses DllImport/PInvoke to call the same function it runs fine:
namespace ConsoleApplication3
{
class Program
{
[DllImport("ExternalModule.dll")]
public static extern int external_method(int n);
static void Main(string[] args)
{
external_method(0);
Debug.WriteLine("YES!");
}
}
}
I'm struggling to understand how one can work and the other fail. Unfortunately I have no access to the developer of the C DLL.
Any help much appreciated.
Update Thanks for the comments. Here is the header file entry for the method:
int __stdcall exernal_method(int cardno);
Am reasonably sure that the calling convention is ok. The DLL is being loaded because using the debug version of the DLL I can see it output some messages to the debug output before it fails.
Could it be an issue with stack being corrupted on exit of the method? I tried a couple of different calling conventions but this is the only one that the linker was able to work with.
Stack corruption is unlikely.
One possible reason is different DLL loading method. Your C++ app loads it statically (Windows loads your DLL before the process launched), C# dynamically (CLR loads the DLL after the process started to run). To test this hypothesis, remove ExternalModule.lib from linker, change your C++ code to call LoadLibrary and GetProcAddress instead.
Another possible reason is C# runtime initializes COM, C++ app doesn’t, and your DLL tried to use COM. In your C++ app, try to add CoInitialize[Ex] before your exernal_method.

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

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!

Errors thrown from stl when compiling a module which uses the "Meschach" library

I'm working on a module which uses a shared library, which in turn has a static library linked to it. The shared library build works fine and generates a .so. When I try to use it in the module, I get a variety of errors, most of which are based on stl (stl collections to be specific), at the compilation stage. The errors look like:
In file included from /usr/include/c++/4.3/list:68,
from /home/gayan/LHIMo/LHI/src/CalcEngine/include/JuncNodeInfo.h:11,
from /home/gayan/LHIMo/LHI/src/CalcEngine/include/RiverFlowParameter.h:11,
from Main.cpp:11:
/usr/include/c++/4.3/bits/stl_list.h:465:11: error: macro "catch" requires 3 arguments, but only 1 given
This is given in most places which use list, vector or map.
Please help me to resolve this.
Sample code: "CalcEngine.h" in the library:
#ifndef LHI_CALCENGINE_H_
#define LHI_CALCENGINE_H_
extern "C"{
#include <matrix2.h>
}
class CalcEngine{
public:
protected:
};
#endif /* LHI_CALCENGINE_H_ */
Main.cpp in the application:
#include <iostream>
#include <CalcEngine.h>
#include <list> // The compilation fails as soon as this is added
int main(int argc, char** argv){
return -1;
}
I feel this has something to do with the matrix2.h file but could not pinpoint it. The file could be found here
Doing some googling it seems like the Meschach library has a macro called catch (defined err.h indirectly included by matrix2.h) causing c++ code having exception catching to fail.
Try
#undef catch
after you are done including the meschach headers and see if works better.