How to get debug interface for directx 11 in C - c

Trying to follow this example:
DirectX11 ReportLiveObjects Instantiation
I'm trying to get a debug interface setup using C99 with directx11. I've already defined COBJMACROS and CINTERFACE in order to use directx c api and currently have a triangle rendering on my screen. In order to get dxgi debug stuff setup I've tried:
IDXGIDebug1* debug = { 0 };
DXGIGetDebugInterface(0, IID_PPV_ARGS(&debug));
IDXGIDebug1_ReportLiveObjects(debug, DXGI_DEBUG_ALL, DXGI_DEBUG_RLO_ALL);
but get errors that 'IID_PPV_ARGS' is not defined and I'm sure this is because I'm using the C interface and this is specific to c++. I've also tried:
IDXGIDebug1* debug = { 0 };
ID3D11Device_QueryInterface(device, &IID_IDXGIDebug1, (void**)(&debug));
IDXGIDebug1_ReportLiveObjects(debug, DXGI_DEBUG_ALL, DXGI_DEBUG_RLO_ALL);
But 'debug' variable isn't getting filled. Not sure what the equivalent C calls are for DXGI debug interface.

After some diggin I found what I was looking for. The c function equilavent 'DXGIGetDebugInterface1' is located in the dxgi1_3.h header file and I was only including the dxgidebug.h header. Also, need to link to dxgi.lib. Final code is:
IDXGIDebug1* debug = { 0 };
DXGIGetDebugInterface1(0, &IID_IDXGIDebug1, (void**)(&debug));
IDXGIDebug1_ReportLiveObjects(debug, DXGI_DEBUG_ALL, DXGI_DEBUG_RLO_ALL);

Related

How to read in files with a specific file ending at compile time in nim?

I am working on a desktop application using nim's webgui package, which sort of works like electron in that it renders a gui using HTML + CSS + JS. However, instead of bundling its own browser and having a backend in node, it uses the browser supplied by the OS (Epiphany under Linux/GNOME, Edge under Windows, Safari under iOS) and allows writing the backend in nim.
In that context I am basically writing an SPA in Angular and need to load in the HTML, JS and CSS files at compile-time into my binary.
Reading from a known absolute filepath is not an issue, you can use nim's staticRead method for that.
However, I would like to avoid having to adjust the filenames in my application code all the time, e.g. when a new build of the SPA changes a file name from main.a72efbfe86fbcbc6.js to main.b72efbfe86fbcbc6.js.
There is an iterator in std/os that you can use at runtime called walkFiles and walkPattern, but these fail when used at compileTime!
import std/[os, sequtils, strformat, strutils]
const resourceFolder = "/home/philipp/dev/imagestable/html" # Put into config file
const applicationFiles = toSeq(walkFiles(fmt"{resourceFolder}/*"))
/home/philipp/.choosenim/toolchains/nim-#devel/lib/pure/os.nim(2121, 11) Error: cannot 'importc' variable at compile time; glob
How do I get around this?
Thanks to enthus1ast from nim's discord server I arrived at an answer: using the collect macro with the walkDir iterator.
The walkDir iterator does not make use of things that are only available at runtime and thus can be safely used at compiletime. With the collect macro you can iterate over all your files in a specific directory and collect their paths into a compile-time seq!
Basically you start writing collect-block, which is a simple for-loop that at its end evaluates to some form of value. The collect macro will put them all into a seq at the end.
The end result looks pretty much like this:
import std/[sequtils, sugar, strutils, strformat, os]
import webgui
const resourceFolder = "/home/philipp/dev/imagestable/html"
proc getFilesWithEnding(folder: string, fileEnding: string): seq[string] {.compileTime.} =
result = collect:
for path in walkDir(folder):
if path.path.endswith(fmt".{fileEnding}"): path.path
proc readFilesWithEnding(folder: string, fileEnding: string): seq[string] {.compileTime.} =
result = getFilesWithEnding(folder, fileEnding).mapIt(staticRead(it))

Handling same name symbols in C

Suppose that I have an app written in C that must parse some blob data. The parsing process is done by an entire dedicated library (I have access to this library code).
Since this blob is versioned I need to support let's say 2 versions of it at a time. Workflow would be something like:
'v1' is out -> library supports 'v1'
'v2' is out -> library now supports 'v2' and 'v1'
'v3' is out -> library now supports 'v3' and 'v2'
and so on.
The main problem is that the majority of symbols in the library for 'v2', for example, are also present in 'v1' library, with the same function prototype.
First thought was use something like namespaces in C++, in order to have something like:
/app/src/lib_v1/parser.c
void _parse_blob(char* blob){ //data parsing code for v1}
/////////////////////////////
/app/src/lib_v2/parser.c
void _parse_blob(char* blob){ //data parsing code for v2}
////////////////////////////
/app/src/main.c
//Pseudo-code
char* data;
if(_check_version(data) == 'v1')
parser.v1._parse_blob(data);
else
parser.v2._parse_blob(data);
But I don't know if any similar can be achieved in C without changing anything in the code of the 'outgoing' library (v1 in this case) since all that code has already been tested and modifying it would invalidate all the release tests.
Other idea would be separate both libraries code into two dynamic linked libraries and load/unload them when necessary, but don't know if could work or if it is efficient.
What would be the best approach to this problem ?

LuaJIT FFI: Uploading Steamworks leaderboards

How to use SteamAPICall_t with a SteamLeaderboard_t handle with LuaJIT FFI?
I use LÖVE2D framework & Steamworks Lua Integration (SLI)
Links: FindLeaderboard
/ UploadLeaderboardScore
/ Typedef
function UploadLeaderboards(score)
local char = ffi.new('const char*', 'Leaderboard name')
local leaderboardFound = steamworks.userstats.FindLeaderboard(char) -- Returns SteamAPICall_t
local leaderboardCurrent = ?? -- Use SteamAPICall_t with typedef SteamLeaderboard_t somehow.
local c = ffi.new("enum SteamWorks_ELeaderboardUploadScoreMethod", "k_ELeaderboardUploadScoreMethodKeepBest")
score = ffi.cast('int',math.round(score))
return steamworks.userstats.UploadLeaderboardScore(leaderboardCurrent, c, score, ffi.cast('int *', 0), 0ULL)
end
leaderboardCurrent = ffi.cast("SteamLeaderboard_t", leaderboardFound) -- No declaration error
SteamAPICall_t is simply a number that corresponds to your request.
This is meant to be used alongside CCallback in the steam API.
The lua integration misses out CCallback and STEAM_CALLBACK.
The SteamLeaderboard_t response is generated by calling FindLeaderboard.
In this case you are making a request to steam and steam needs to respond in an asynchronous way.
So what you have to do is define a Listener object ( in C++ ) that will listen for the response ( which will be in form of SteamLeaderboard_t) and write C-like functions for it so ffi can understand them.
This means that your program must be able to do this:
Register a listener for the leaderboard.
Submit a request for a leaderboard. ( FindLeaderboard )
Wait for message ( SteamLeaderboard_t )
Use SteamLeaderboard_t
In short you will need to write code in C++ for the events and add C-like interface for them and compile it all into a DLL then link that DLL to lua using FFI. This can be tricky so exercise caution.
in C (ffi.cdef and dll):
//YOU have to write a DLL that defines these
typedef struct LeaderboardEvents{
void(*onLeaderboardFound)(SteamLeaderboard_t id);
} LeaderboardEvents;
void MySteamLib_attachListener(LeaderboardEvents* events);
Then in lua.
local lib = --load your DLL library here
local Handler = ffi.new("LeaderboardEvents")
Handler.onLeaderboardFound = function(id)
-- do your stuff here.
end
lib.MySteamLib_attachListener(Handler)
While writing your DLL, I STRONGLY recommend that you read through the SpaceWar example provided by steam api so you can see how callbacks are registered.

store all the errors occurred on eval() in PHP

Stack community.
I'm using the eval() function in PHP so my users can execute his own code in my website (Yes, i know it is a dangerous function, but that's not the point).
I want to store all the PHP errors that occur during the interpretation of the code, is there a way to fetch all of them? i want to get and register them in a table of my database.
The error_get_last gets only the last error, but i want all of them.
Help me, please. It is even possible?
General
You cannot use eval() for this, as the evaled code will run in the current context, meaning that the evaled code can overwrite all vars in your context. Beside from security considerations this could / would break functionality. Check this imaginal example:
$mode = 'execute'
// here comes a common code example, it will overwrite `$mode`
eval('
$mode = 'test';
if(....) { ...
');
// here comes your code again, will fail
switch ( $mode) {
...
}
Error Tracking
You cannot track the errors this way. One method would be to use set_error_handler() to register a custom error handler which stores the errors to db. This would work, but what if the user uses the function in it's code? Check the following examples:
set_error_handler('my_handler');
function my_handler($errno, $errstr, $errfile, $errline) {
db_save($errstr, ...);
}
eval('
$a = 1 / 0; // will trigger a warning
echo $b; // variable not defined
'
);
This would work. But problems will arise if have an evaled code like this:
eval('
restore_error_handler();
$a = 1 / 0; // will trigger a warning
echo $b; // variable not defined
'
);
Solution
A common solution to make it possible that others can execute code on your servers is:
store user code into temporary file
disable critical functions like fopen() ... in the php.ini
execute the temporary php file by php-cli and display output (and errors) to the user
if you separate stdin from stdout when calling the php-cli, you can parse the error messages and store them in a DB
According to the documentation, you just can't :
If there is a parse error in the evaluated code, eval() returns FALSE and execution of the following code continues normally. It is not possible to catch a parse error in eval() using set_error_handler().
EDIT: you can't do it with eval(), but you apparently can with php_check_syntax function. You have to write the code to a file in order to check its syntax.

Apache filter request hostname comparison does not work

I'm new to writing apache output filters and even more to writing them in c. I've taken a sample filter (mod_substitute) source and am trying to make it work only if the request is for a specific host. I need to make this a programatic feature and not configuration wise for my own reasons.
I am using the following code to try this:
request_rec *req = f->r;
ngf_module_ctx *ctx = f->ctx;
/* Test to see if this is a domain that needs optimization */
if (req->hostname != "localhost") {
ap_pass_brigade(f->next, bb);
return APR_SUCCESS;
}
using debug printing I saw that the req->hostname value does show up as localhost, however the comparison itself fails.
what am I doing wrong ?
You're using a pointer equality operator (!=) to compare strings. This is C, remember -- you have to use a function like strcmp to compare strings.
If you haven't worked in C before, writing an Apache module is probably not a great place to start. Back up and get familiar with the language first.

Resources