0xC0000005: Access violation reading location 0x00000000 DirectX 10 - crash

I am trying to get a render target for my swap chain using DirectX 10. For some reason, when I add in the GetBuffer line, it spits out errors. Here's my file:
#include <Windows.h>
#include <stdlib.h>
#include <string.h>
#include <tchar.h>
#include <d3d10.h>
#include <D3DX10.h>
#pragma comment (lib, "d3d10")
//WINDOWS MAIN FUNCTION
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow);
//WINDOW PROCEDURE FUNCTION
//RESPONDS TO EVENTS
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
//Global variables
//The main window class name.
static TCHAR szWindowClass[] = _T("virhackapp");
// The string that appears in the application's title bar.
static TCHAR szTitle[] = _T("VirHack");
HINSTANCE hInst;
//DirectX Vars
DXGI_SWAP_CHAIN_DESC *SwapChain;
IDXGISwapChain **ppSwapChain;
ID3D10Device **device;
void **ppSurface;
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
//LAST THREE ARE OUTPUTS, SEE VARS ABOVE
D3D10CreateDeviceAndSwapChain(NULL,D3D10_DRIVER_TYPE_HARDWARE, NULL, D3D10_SDK_VERSION, D3D10_SDK_VERSION, SwapChain, ppSwapChain, device);
(*ppSwapChain)->GetBuffer(0, __uuidof(ID3D10Texture2D), ppSurface);
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_APPLICATION));
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = NULL;
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_APPLICATION));
if(!RegisterClassEx(&wcex))
{
MessageBox(NULL, _T("Call to RegisterClassEx failed!"), _T("virhackapp"), NULL);
return 1;
}
hInst = hInstance;
//The parameters to CreateWindow explained:
//szWindowClass: The name of the app
//szTitle: the text taht appears in the title bar
//WS_OVERLAPPEDWINDOW: the type of the window to create
//CW_USEDEFAULT, CW_USEDEFAULT: initial position(x,y)
//500,100: initial size(width, length)
//NULL: the parent of this window
//NULL: this application does not have a menu bar
//hInstance: thie first parameter from WinMain
//NULL: not used in this application
HWND hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 500, 100, NULL, NULL, hInstance, NULL);
if(!hWnd)
{
MessageBox(NULL, _T("Call to CreateWindow failed!"), _T("virhackapp"), NULL);
return 1;
}
//DISPLAYS WINDOW
//The parameters to ShowWindow explained:
//hWnd: the value returned from CreateWindow
//nCmdShow: the fourth parameter from WinMain
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
//MESSAGE LOOP THAT SENDS TO WndProc
MSG msg;
while(GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (int)msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
//WM_PAINT: app recieves this message when part of its displayed window must be updated. (When the window is first displayed, all of it must be updated)
//To handle WM_PAINT, first call BeginPaint, then handle all logic to lay out text, buttons, and other controls
//Then call EndPaint
//Use TextOut to display a string
PAINTSTRUCT ps;
HDC hdc;
TCHAR greeting[] = _T("Hello, World!");
switch(message)
{
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
//Here the application is laid out.
//For right now, displays "Hello, World!"
//in the top left corner.
//Graphics are done here
TextOut(hdc, 5, 5, greeting, _tcslen(greeting));
//End application specific layout section
EndPaint(hWnd, &ps);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
break;
}
return 0;
}
The erroneous line is here:
(*ppSwapChain)->GetBuffer(0, __uuidof(ID3D10Texture2D), ppSurface);
I suspect this is happening because my CreateDeviceAndSwapChain is not successfully completing, but I have no idea why. Here is the actual error report:
'VirHack.exe': Loaded 'C:\Users\Will\Documents\Visual Studio 2010\Projects\VirHack\Debug\VirHack.exe', Symbols loaded.
'VirHack.exe': Loaded 'C:\Windows\SysWOW64\ntdll.dll', Cannot find or open the PDB file
'VirHack.exe': Loaded 'C:\Windows\SysWOW64\kernel32.dll', Cannot find or open the PDB file
'VirHack.exe': Loaded 'C:\Windows\SysWOW64\KernelBase.dll', Cannot find or open the PDB file
'VirHack.exe': Loaded 'C:\ProgramData\Norton\{0C55C096-0F1D-4F28-AAA2-85EF591126E7}\NAV_20.2.0.19\Definitions\BASHDefs\20130620.001\UMEngx86.dll', Cannot find or open the PDB file
'VirHack.exe': Loaded 'C:\Windows\SysWOW64\user32.dll', Cannot find or open the PDB file
'VirHack.exe': Loaded 'C:\Windows\SysWOW64\gdi32.dll', Cannot find or open the PDB file
'VirHack.exe': Loaded 'C:\Windows\SysWOW64\lpk.dll', Cannot find or open the PDB file
'VirHack.exe': Loaded 'C:\Windows\SysWOW64\usp10.dll', Cannot find or open the PDB file
'VirHack.exe': Loaded 'C:\Windows\SysWOW64\msvcrt.dll', Cannot find or open the PDB file
'VirHack.exe': Loaded 'C:\Windows\SysWOW64\advapi32.dll', Cannot find or open the PDB file
'VirHack.exe': Loaded 'C:\Windows\SysWOW64\sechost.dll', Cannot find or open the PDB file
'VirHack.exe': Loaded 'C:\Windows\SysWOW64\rpcrt4.dll', Cannot find or open the PDB file
'VirHack.exe': Loaded 'C:\Windows\SysWOW64\sspicli.dll', Cannot find or open the PDB file
'VirHack.exe': Loaded 'C:\Windows\SysWOW64\cryptbase.dll', Cannot find or open the PDB file
'VirHack.exe': Loaded 'C:\Windows\SysWOW64\d3d10.dll', Cannot find or open the PDB file
'VirHack.exe': Loaded 'C:\Windows\SysWOW64\d3d10core.dll', Cannot find or open the PDB file
'VirHack.exe': Loaded 'C:\Windows\SysWOW64\dxgi.dll', Cannot find or open the PDB file
'VirHack.exe': Loaded 'C:\Windows\SysWOW64\version.dll', Cannot find or open the PDB file
'VirHack.exe': Loaded 'C:\Windows\SysWOW64\dwmapi.dll', Cannot find or open the PDB file
'VirHack.exe': Loaded 'C:\Windows\SysWOW64\d3d11.dll', Cannot find or open the PDB file
'VirHack.exe': Loaded 'C:\Windows\SysWOW64\msvcr100d.dll', Symbols loaded.
'VirHack.exe': Loaded 'C:\Windows\SysWOW64\imm32.dll', Cannot find or open the PDB file
'VirHack.exe': Loaded 'C:\Windows\SysWOW64\msctf.dll', Cannot find or open the PDB file
'VirHack.exe': Loaded 'C:\Windows\SysWOW64\nvinit.dll', Cannot find or open the PDB file
'VirHack.exe': Loaded 'C:\Program Files (x86)\NVIDIA Corporation\coprocmanager\detoured.dll', Cannot find or open the PDB file
'VirHack.exe': Loaded 'C:\Program Files (x86)\NVIDIA Corporation\coprocmanager\Nvd3d9wrap.dll', Cannot find or open the PDB file
'VirHack.exe': Loaded 'C:\Windows\SysWOW64\setupapi.dll', Cannot find or open the PDB file
'VirHack.exe': Loaded 'C:\Windows\SysWOW64\cfgmgr32.dll', Cannot find or open the PDB file
'VirHack.exe': Loaded 'C:\Windows\SysWOW64\oleaut32.dll', Cannot find or open the PDB file
'VirHack.exe': Loaded 'C:\Windows\SysWOW64\ole32.dll', Cannot find or open the PDB file
'VirHack.exe': Loaded 'C:\Windows\SysWOW64\devobj.dll', Cannot find or open the PDB file
'VirHack.exe': Loaded 'C:\Program Files (x86)\NVIDIA Corporation\coprocmanager\nvdxgiwrap.dll', Cannot find or open the PDB file
First-chance exception at 0x00831056 in VirHack.exe: 0xC0000005: Access violation reading location 0x00000000.
Unhandled exception at 0x778e15de in VirHack.exe: 0xC0000005: Access violation reading location 0x00000000.
The program '[10756] VirHack.exe: Native' has exited with code -1073741819 (0xc0000005).
Any ideas of why this is happening?

Related

Open a file in Emscripten using browser file selector dialogue

I am writing a C++ program to compile to web assembly with Emscripten, to run in the browser.
What's the simplest way to cause the user's browser to open a file selector dialogue for file upload, and to access the contents of that file from C++?
I don't want to have to add a visible element to the page containing the wasm canvas if possible, ideally the process should be triggered from the C++ program itself.
UPDATE: I have now released a pure-C++ header-only library to offer file "uploads" and "downloads" in Emscripten:
https://github.com/Armchair-Software/emscripten-browser-file (MIT license)
There are a number of ways to do this, but by far the cleanest (requiring least code, and no requirement for complex use of the Emscripten filesystem API) is as follows:
In your build system, add -sEXPORTED_RUNTIME_METHODS=[ccall] to export ccall, allowing you to call C functions from javascript.
In your html script section, add the following function - this creates a FileReader which reads a file from a file input, copies it to the heap, and passes the address and size to a C function you define:
var open_file = function(e) {
const file_reader = new FileReader();
file_reader.onload = (event) => {
const uint8Arr = new Uint8Array(event.target.result);
const num_bytes = uint8Arr.length * uint8Arr.BYTES_PER_ELEMENT;
const data_ptr = Module._malloc(num_bytes);
const data_on_heap = new Uint8Array(Module.HEAPU8.buffer, data_ptr, num_bytes);
data_on_heap.set(uint8Arr);
const res = Module.ccall('load_file', 'number', ['number', 'number'], [data_on_heap.byteOffset, uint8Arr.length]);
Module._free(data_ptr);
};
file_reader.readAsArrayBuffer(e.target.files[0]);
};
Somewhere in your C++ code, define a C function to process the file contents:
extern "C" {
EMSCRIPTEN_KEEPALIVE int load_file(uint8_t *buffer, size_t size) {
/// Load a file - this function is called from javascript when the file upload is activated
std::cout << "load_file triggered, buffer " << &buffer << " size " << size << std::endl;
// do whatever you need with the file contents
return 1;
}
}
EMSCRIPTEN_KEEPALIVE and extern "C" are both needed here; EMSCRIPTEN_KEEPALIVE also adds the function to exports, so you do not need to export it manually.
Finally, when you want to pop open the file selector dialogue, to prompt the user to choose a file, insert the following in your C++ code:
#include <emscripten.h>
...
EM_ASM(
var file_selector = document.createElement('input');
file_selector.setAttribute('type', 'file');
file_selector.setAttribute('onchange','open_file(event)');
file_selector.setAttribute('accept','.png,.jpeg'); // optional - limit accepted file types
file_selector.click();
);
This creates a file selector input element with the properties you specify, and immediately simulates a click to it - this method lets you use buttons in your own interface, or other C++ logic, without relying on the browser rendering of an input element.

Reflective DLL injected code crashing on ofstream.open()

I am trying to load a library via reflective DLL injection, but it crashes on calling ofstream.open(); calling fopen() yields the same result.
For reflective dll injection i basically follow the steps described at https://0x00sec.org/t/reflective-dll-injection/3080:
Load the dll contents in a buffer
Create a file mapping and map view of file, then copying the NT headers and sections there.
Copy the contents of the mapping to a buffer and close the mapping.
Rebuild the import table.
Rebuild relocations.
Allocate memory in a target suspended process and copy the buffer there.
CreateRemoteThread to start the dllMain (while the target process is still suspended).
The code gets loaded and the dllmain is executed.
My dllmain looks like this:
BOOL APIENTRY DllMain( HMODULE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
logFile.open("hookLog.txt", std::ios::out);
logFile << "DLL Attached\n";
HRESULT nResult = InitCoCreateInstanceHook();
logFile << "Hook initialized with result: " << std::to_string(nResult) << "\n";
logFile.flush();
}
And it crashes while opening the log file. I know exeption handling is not set up when I execute the code, could that be the issue? Am I missing something else?
Thanks in advance.

Ghost script does not reflect the correct information in the PRN file

I create prn code from pdfs and send them to printers in a C# code to automatize printing jobs. To do that, I use ghost script with following parameters.
gswin32c -dNOPAUSE -dBATCH -sDEVICE=laserjet -sOutputFile="c:/temp/out.prn" "NumberedPages.pdf"
This commandline arguments generates a prn file named out.prn. When I send this file to the printer with following C# code, it prints the pdf file successfully.
public static bool SendBytesToPrinter(string szPrinterName, IntPtr pBytes, Int32 dwCount)
{
uint returnedValue = 0;
Int32 dwError = 0, dwWritten = 0;
IntPtr hPrinter = new IntPtr(0);
DOCINFOA di = new DOCINFOA();
bool bSuccess = false; // Assume failure unless you specifically succeed.
di.pDocName = "My C#.NET RAW Document";
di.pDataType = "RAW";
// Open the printer.
if (OpenPrinter(szPrinterName.Normalize(), out hPrinter, IntPtr.Zero))
{
// Start a document.
returnedValue = StartDocPrinter(hPrinter, 1, di);
if (0 != returnedValue)
{
// Start a page.
if (StartPagePrinter(hPrinter))
{
// Write your bytes.
bSuccess = WritePrinter(hPrinter, pBytes, dwCount, out dwWritten);
EndPagePrinter(hPrinter);
}
EndDocPrinter(hPrinter);
}
ClosePrinter(hPrinter);
}
// If you did not succeed, GetLastError may give more information
// about why not.
if (bSuccess == false)
{
dwError = Marshal.GetLastWin32Error();
}
return bSuccess;
}
(https://stackoverflow.com/a/29564132/3079364)
I also want to monitor the print jobs if they are successfully printed by the printer or not. But the prn file which is generated by ghost script does not reflects the correct number of pages. See below
Captured from CZ Print job tracker
When I print this document using Word or Adobe, CZ Print job tracker shows the correct number of pages.
Is there any parameter that I can add to ghost script command to correct this information ?
The output file is simply a PCL file, because that's what laserjet devices understand. This does not contain any information about the number of pages in the file.
The reason the print spooler thinks there's only one page is because you haven't told it differently. You have opened a file, started a page, and spewed the content directly to the printer, there is no way for the spooler to know how many pages that stream contains if you don't tell it where each one starts and stops.
You call StartPagePrinter once, so the print spooler (not unreasonably) assumes there is only one page. If you call StartPagePrinter and EndPagePrinter once for each page, then you will get the correct number of pages. Of course, that means knowing where each page begins and ends in the file output by Ghostscript, which you don't know.
Your best bet would be to use the %d format to OutputFile to produce one file per page, then the page counting will be correct.
There doesn't seem to be any way to tell the print spooler how many pages there are when you are sending data directly to the printer. I guess that's not surprising, since the print spooler isn't actually involved.

Can not generate lib file for shared dll for TAO IDL

One IDL file exception.idl
module project{
exception JobCreateException{
string errorMessage;
};
}
parts of MPC file is:
project(idl_exception): taoidldefaults, anytypecode {
idlflags += -GI -Wb,stub_export_macro=EXCEPTION_STUB_Export -Wb,stub_export_include=exception_stub_export.h -Wb,skel_export_macro=EXCEPTION_SKEL_Export
-Wb,skel_export_include=exception_skel_export.h
IDL_Files {
exception.idl
}
custom_only = 1
}
project(idl_exception_skel): naming, iortable, utils, avoids_corba_e_micro, anytypecode {
sharedname = idl_exception_skel
after += idl_exception
Source_Files {
exceptionS.cpp
}
Header_Files{
exceptionS.h
exceptionC.h
exception_skel_export.h
}
dynamicflags += EXCEPTION_SKEL_BUILD_DLL EXCEPTION_STUB_BUILD_DLL
}
According to mpc file, I want to generate a skeleton DLL files by VC8, and the .lib file would be linked by server implementation.
However, after compilation, the idl_exception_skel.dll file is successfully generated, but no .lib file.
Than i add a new struct like:
struct myobject{
string name;
};
inside of idl file and regenerate all, the lib file shows up.
Is there any explanation for what kinds of IDL file can not be for skeleton?
#Johnny Willemsen
The skel library has to link with the stub library that is related to the IDL file. For the stub project add EXCEPTION_STUB_BUILD_DLL to the dynamicflags, for the skel project only use EXCEPTION_SKEL_BUILD_DLL. The fact that the lib is missing hints at unresolved symbols, which are caused by the fact that you don't add the idl_exception_stub to the libraries of idl_exception_skel.

Program which runs other program - different directories & DLLs files

I'm writing a program and I have problem with running it in the nice way. I use a lot of DLLs and unfortunately they have to be placed in the application directory or in the system folder. I don't want to put it in the system directories and that's why I need to put it in the same directory as my .exe file. Still I don't want the user to look thorugh many many DLLs files to find the .exe. That's why I thought that I could run the .exe with the use of different program which is located in different place(in the same directory as the folder with my .exe and DLL files). Unfortunately, the program loads files only from the directory of the first program. Here's my code. I would be really grateful if you could help me.
#include <windows.h>
#include <iostream>
std::string ExePath()
{
char buffer[MAX_PATH];
GetModuleFileName( NULL, buffer, MAX_PATH );
std::string::size_type pos = std::string( buffer ).find_last_of( "\\/" );
return std::string( buffer ).substr( 0, pos);
}
int main()
{
const char * filepath;
filepath = (ExePath()+"\\bin\\test.exe").c_str();
STARTUPINFO info={sizeof(info)};
PROCESS_INFORMATION processInfo;
if (CreateProcess(filepath, "cmd", NULL, NULL, TRUE, 0, NULL, NULL, &info, &processInfo))
{
::WaitForSingleObject(processInfo.hProcess, INFINITE);
CloseHandle(processInfo.hProcess);
CloseHandle(processInfo.hThread);
}
return 0;
}