OllyDbg can't debug visual studio exe - c++

I've just created a new vc++ exe with this simple code:
#include<stdio.h>
#include<string.h>
#include<windows.h>
int ExceptionHandler(void);
int main(int argc,char *argv[]){
char temp[512];
printf("Application launched");
try
{
throw "error";
}
catch (... )
{
ExceptionHandler();
}
return 0;
}
int ExceptionHandler(void)
{
printf("Exception");
return 0;
}
The app is extremely simple, and an exe file depending on kernel32.dll and MSVCR100D.dll is created.
When I try to import and debug it into OllyDbg (I just wanted to see the SEH chain in the stack window) it says "Module 'testseh' has entry point outside the code (as specified in the PE header). Maybe this file is self-extracting or self-modifying. Please keep it in mind when setting breakpoints!" and no code is executed, it jumps directly to the ntdll.dll crash part (in fact the exe is crashing but I can't step by step the printf instructions)
How come this behaviour? The exe doesn't rely on CLI neither CLR, am I missing something?

Compiler exceptions taken from olly as critical (wrong settings)

Related

Wt c++ critical error when creating WApplication object

I have problem with my Wt web application. It's rather simple app, I do not need to deploy it on any external server (only localhost), so built in whttpd server provided by framework is sufficient for my needs. I create an executable file in release mode (Visual Studio 2015), run it, and when I open localhost:8080 in browser to access application, I get an error. In debug mode however everything works well.
Debug console shows this:
Exception thrown at 0x00007FFB08477788 in
Neural_network_visualisation.exe: Microsoft C++ exception:
std::runtime_error at memory location 0x00000025E30FB408.
Critical error detected c0000374
Neural_network_visualisation.exe has triggered a breakpoint.
main.cpp
#include "MyApplication.h"
#include "MyContainerWidget.h"
WApplication *createApplication(const WEnvironment& env) //exception is thrown here
{
Wt::WApplication *app = new Wt::WApplication(env); //error c0000374
app->setCssTheme("polished");
new MyContainerWidget(app->root());
return app;
}
int main(int argc, char **argv)
{
return WRun(argc, argv, &createApplication);
}
The exception is thrown just after entering createApplication function, but the program doesn't crash there. After executing first line critical error is shown and application stops.
The code is so simple that I can't see any problem with it. My guess is that release mode expects some special configuration to work with Wt, but official documentation doesn't mention anything more is needed while using built-in http server. Can anybody with Wt experience help me with this?
Edit 1:
I changed my code so it looks like this:
#include "MyApplication.h"
#include "MyContainerWidget.h"
WApplication *createApplication(const WEnvironment& env)
{
//Wt::WApplication *app =
// app->setCssTheme("polished");
//new MyContainerWidget(app->root());
return new Wt::WApplication(env);;
}
int main(int argc, char **argv)
{
return WRun(argc, argv, &createApplication);
}
Now the error is different and states:
HEAP[Neural_network_visualisation.exe]: Invalid address specified to
RtlValidateHeap( 000002A04F550000, 000002A05117F060 )
So this is a memory management problem. In debug mode this management works quite different than in release, that's why I do not get any error while in debug. Unfortunately, I still do not know how to fix this. Any ideas?
Problem solved. I still can't exactly tell what was the cause, but I recompiled Wt and Boost libraries and changed project properties to match with the Wt examples. I also had to change build type from x64 to x86. It works now.

Output to windows cmd prompt

I have been told to make an exe file that will output 5 lines. I have been given these instructions:
Your program must not hang after running. I will execute it at command-line and will expect it to finish when the program is done.
I tried going to properties -> linker -> system - > console. It seemed to do what he wanted when I ran ctrl-f5. I then made an exe and now it disappears, and it doesn't print to the console.
I am using a simple cout program ex:
int main()
{
cout<<"hello"<<endl;
return 1;
}
Edit
I'm using visual studio 2013, and I am running from the command line.
Note I'm not asking for the window to stay open, but printing to the console itself.
I am not pressing ctrl-f5, but going to cmd.exe and then to the executable. I have tried the release version as well as the debug version.
If I have understood your question. Try following and add useful parts to your code. Not needed lines are commented out.
// ConsoleApplication1.cpp : Defines the entry point for the console application.
// (Just 2 ways to wait, befoore command-line console vanishes.)
#include "stdafx.h"
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
cout << "It's Foo's Bar!\n";
system("pause"); //preferred way.
//cin.get(); //2nd way, not as good.
return 0;
}

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.

Boost thread and UPX compression == not valid win32 application?

When I just declare
boost::thread t1, t2;
in my program and then compress .exe file with UPX, the compression succeeds. But when I try to launch the compressed exe, Windows tells me that it's "not valid win32 application".
There is a bug report for UPX (similar bug), but it has different error message ("The application failed to initialize properly (0xc0000005)").
In my case OS thinks the file is corrupted or something, so it cant even be started to show errors! Why??
Win7x64, C++, VisualStudio, boost 1.47, UPX3.07
strange news:
Unpacking exe makes corrupted exe that throws error exactly the same
as here. ("The application failed to initialize properly
(0xc0000005)") And this is for unpacked exe, not packed as in bug
report.
extern "C" void tss_cleanup_implemented(void) {}
before the inclusion of boost's thread header does not matter. The
result is the same.
main.cpp:
#include <boost/thread.hpp>
int main(int argc, char** argv)
{
boost::thread t;
return 0;
}
May be someone will try to compile and compress?
Bug was repaired in new version 3.08. It's ok now.

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!