How to call DXGI from C without __uuidof - c

I'm unsure of how to get DXGI GUIDs without the C++-only __uuidof operator. It's used all over the MSDN code. I'm also unsure of the syntax of calling OO Windows APIs in C.
I've checked on MSDN and here, but found nothing.
#ifdef __cplusplus
CreateDXGIFactory(__uuidof(IDXGIFactory), (void **)&factory);
factory->EnumAdapters(0, &adapter);
adapter->EnumOutputs(0, &output);
#else // C version, not correct AFAIK
?? CreateDXGIFactory(&IID_IDXGIFactory, (void **)&factory);
?? IDXGI_EnumAdapters(factory, 0, &adapter);
?? IDXGI_EnumOutputs(adapter, 0, &output);
#endif
The first works, but the C version fails with unresolved, DXGI-related externals (EnumAdapters and EnumOutputs and IID_DXGIFactory).

This should be fine:
#define COBJMACROS
#include <dxgi.h>
void test()
{
IDXGIFactory* factory;
if (SUCCEEDED(CreateDXGIFactory(&IID_IDXGIFactory, (void**) &factory)))
{
IDXGIAdapter* adapter;
if (SUCCEEDED(IDXGIFactory_EnumAdapters(factory, 0, &adapter)))
{
// ...
}
}
}

Related

ISR documentation with doxygen

I have some ISR's and I do not really know how to documentate them by doxygen.
Do you have an idea?
Here an example. Is there any special code by doxygen to documentate this?
ISR(usartTimer_OVF_vect){
usartData.flag_send_data = TRUE;
}
Directly it is not possible as it is not valid C, but with workaround it could be possible.
#if defined(__DOXYGEN__)
void usartTimer_OVF_Vect(void)
#else
ISR(usartTimer_OVF_vect)
#endif
{
usartData.flag_send_data = TRUE;
}
In this case, doxygen will simply see a void function with usartTImer_OVF_Vect name without any parameters.

implement errno analogue using pthread

As $ man errno says, "errno is defined by the ISO C standard to be a modifiable lvalue of type int, and must not be explicitly declared; errno may be a macro. errno is thread-local; setting it in one thread does not affect its value in any other thread".
I'm developing a C library which will work both in POSIX and Windows, so instead of using errno and GetLastError/SetLastError I've decided to stick to my own error type. Every my function returns error code as cg_error object, where cg_error is just a typedef. However, for some functions like custom allocators it is still better to use something like errno, but with my own cg_error type.
AFAIK errno in glibc is implemented this way:
#define errno (*__errno_location ())
I'm trying to implement a similar function using pthreads on Linux and TlsAlloc and friends for Windows. Here is what I have now (yet only POSIX, seems to be a Solaris implementation from article "Thread-Specific Storage pattern" found on the Web):
cg_error * CG_ERRNO_TLS(void)
{
#if CG_FEATURE_POSIX
static int once;
static pthread_key_t key;
static pthread_mutex_t lock;
cg_error * error = NULL;
if (once)
{
pthread_mutex_lock(&lock);
if (once)
{
(void) pthread_key_create(&key, cg_free);
once = 1;
}
pthread_mutex_unlock(&lock);
}
error = pthread_getspecific(key);
if (!error)
{
error = cg_malloc(sizeof(*error));
(void) pthread_setspecific(key, error);
}
return error;
#endif
}
#define cg_errno (*CG_ERRNO_TLS())
However, when I try to set or get cg_errno, its int value is 6344768, which is not what I want. What am I doing wrong? What's the right way to define something like errno? Thanks in advance!
P.S. I know that I could use __thread and __declspec(thread), but such things are compiler-specific (and probably system specific; I've heard that __thread doesn't work e.g. for MacOSX with gcc).
P.P.S. Base value of cg_error is CG_ERROR_NONE, which is always 0.
UPDATE:
#if CG_FEATURE_POSIX
static pthread_key_t cg_errno_key;
static pthread_once_t cg_errno_once = PTHREAD_ONCE_INIT;
static void cg_errno_init(void)
{ (void) pthread_key_create(&cg_errno_key, cg_free); }
cg_error * cg_errno_storage(void)
{
cg_error * error = NULL;
(void) pthread_once(&cg_errno_once, cg_errno_init);
error = pthread_getspecific(cg_errno_key);
if (!error)
{
error = cg_malloc(sizeof(*error));
(void) pthread_setspecific(cg_errno_key, error);
}
return error;
}
#define cg_errno (*cg_errno_storage())
#endif
Your condition on once is wrong. It should be !once.
But using an int for that is not guaranteed to work. There is a special type pthread_once_t with function pthread_once for the task that you are trying to achieve.
Also, lock should have an initializer, PTHREAD_MUTEX_INITIALIZER.
A use of pthread_once_t would look like this:
static pthread_key_t key;
static pthread_once_t once = PTHREAD_ONCE_INIT;
static
void init_error_key_once(void) {
pthread_key_create(&key, cg_free);
}
cg_error * CG_ERRNO_TLS_POSIX(void)
{
cg_error * error = NULL;
pthread_once(&once, init_error_key_once);
...
}

Can't Understand Weird C Runtime Error. Need Help?

I am trying to master the GObject Library. So I tried to make a simple Gtk+ Custom Widget by inheriting from GtkHBox. I can't figure out what the problem is or even where the problem is so I'll have to paste the entire code. Here is the code:
notetab.h
#ifndef NOTETAB_H
#define NOTETAB_H
G_BEGIN_DECLS
#define PRO_NOTE_TAB(obj) GTK_CHECK_CAST(obj, pro_note_tab_get_type (), ProNoteTab)
#define GTK_CPU_CLASS(klass) GTK_CHECK_CLASS_CAST(klass, pro_note_tab_get_type(), ProNoteTabClass)
#define GTK_IS_CPU(obj) GTK_CHECK_TYPE(obj, pro_note_tab_get_type())
typedef struct _ProNoteTab ProNoteTab;
typedef struct _ProNoteTabClass ProNoteTabClass;
struct _ProNoteTab
{
GtkWidget hbox;
GtkObject parent_instance;
GtkLabel label;
GtkButton cbtn;
};
struct _ProNoteTabClass
{
GtkHBoxClass parent_class;
};
GtkType pro_note_tab_get_type(void);
GtkWidget* pro_note_tab_new(void);
G_END_DECLS
#endif
notetab.c
#include "common.h"
#include "notetab.h"
GtkType pro_note_tab_get_type()
{
GtkType pro_note_tab_type = 0;
if (!pro_note_tab_get_type)
{
static const GtkTypeInfo pro_note_tab_info =
{
"ProNoteTab",
sizeof(ProNoteTab),
sizeof(ProNoteTabClass),
(GtkClassInitFunc) NULL,
(GtkObjectInitFunc) NULL,
NULL,
NULL,
(GtkClassInitFunc) NULL
};
pro_note_tab_type = gtk_type_unique(GTK_TYPE_WIDGET, &pro_note_tab_info);
}
return pro_note_tab_type;
}
GtkWidget* pro_note_tab_new(void)
{
return GTK_WIDGET(gtk_type_new(pro_note_tab_get_type()));
}
Now the program compiles perfectly fine. But the error I get at runtime is:
GTK_CRITICAL**: IA__gtk_type_new : assertion GTK_TYPE_IS_OBJECT(type) failed
GTK_CRITICAL**: IA__gtk_container_add : assertion GTK_IS_WIDGET(widget) failed
What am I doing wrong? Or even I what in the world is this error about?
According to the docs, gtk_type_unique is "deprecated and should not be used in newly-written code".
Use g_type_register_static instead. More so if you are trying to master GObject, not old Gtk+.
Anyway, I'd say that your error is due to some of the NULL function pointers you are setting, some are probably not optional, but this is poorly documented.
For one, the pro_note_tab_type variable in pro_note_tab_get_type() really looks like it should be static.
That must be the problem
if (!pro_note_tab_get_type)
{

Library initialization -- pthread_once in Win32 implementation

Hello. I am trying to make a fully thread-safe initialization function for my library and I couldn't easily find an alternative to pthread_once, which should solve the problem very easily. I've come to this code:
void libInit (void)
{
#ifdef WIN32
static volatile int initialized = 0;
static HANDLE mtx;
if (!initialized)
{
if (!mtx)
{
HANDLE mymtx;
mymtx = CreateMutex(NULL, 0, NULL);
if (InterlockedCompareExchangePointer(&mtx, mymtx, NULL) != NULL)
CloseHandle(mymtx);
}
WaitForSingleObject(mtx);
if (!initialized)
{
libInitInternal();
initialized = 1;
}
ReleaseMutex(mtx);
}
#else
static pthread_once_t initialized = PTHREAD_ONCE_INIT;
pthread_once(&initialized, libInitInternal);
#endif
}
The libInitInternal() call leads to a thread-unsafe function, that initializes the library.
I would like to hear any suggestions on what I could be doing wrong or whether you know about a better solution.
I think you want to use the One-Time Initialization functionality. In synchronous mode, all threads block until the first thread to call it completes. Seems analogous to pthread_once().
There is sample code here.
So in your case, you would say:
BOOL CALLBACK CallLibInitInternal(PINIT_ONCE InitOnce, PVOID Parameter, PVOID *lpContex) {
libInitInternal();
return TRUE;
}
void libInit() {
#ifdef WIN32
static INIT_ONCE s_init_once;
InitOnceExecuteOnce(&s_init_once, CallLibInitInternal, NULL, NULL);
#else
...
#endif
}
You might want to check what pthreads-win32 does in its pthread_once() implementaion. or just use that, if that proves to be easier.
After looking at the following source code for pthread_once() (from here), It looks like you're on the right track.
int pthread_once(pthread_once_t *once_control, void (*init_routine)(void))
{
/* Check first for speed */
if (once_control->state == PTHREAD_NEEDS_INIT) {
pthread_mutex_lock(&(once_control->mutex));
if (once_control->state == PTHREAD_NEEDS_INIT) {
init_routine();
once_control->state = PTHREAD_DONE_INIT;
}
pthread_mutex_unlock(&(once_control->mutex));
}
return(OK);
}
btw, I'll be using pthread_once() to replace some rather convoluted functions in my code.
When using GCC or clang, you can use constructor and destructor attributes. These work for both shared and static libraries, and execute code before and after main is run, respectively. Additionally, you can specify multiple constructor and destructor functions. Much cleaner than the singleton approach, and doesn't require you to remember to call libInit() from your main().
static void __attribute__((constructor))
your_lib_init(void)
{
fprintf(stderr, "library init\n");
}
static void __attribute__((destructor))
vensim_ctx_destroy(void)
{
fprintf(stderr, "library destroy\n");
}
I would check out this article. It is a solution for C++ singletons, but I believe you can use the solution for your code as well: http://www.ddj.com/cpp/199203083?pgno=1
Sadly the listing for the QLock itself is missing, it looks as if they are trying to sell the CD, but there appears to be enough description of it to write one yourself.

Can a macro be used for read-only access to a variable?

Can you define a macro that accesses a normal variable, but in a read-only fashion (other than defining it as a call to a function)? For example, can the VALUE macro in the following code be defined in such a way that the dostuff() function causes a compile error?
struct myobj {
int value;
}
/* This macro does not satisfy the read-only requirement */
#define VALUE(o) (o)->value
/* This macro uses a function, unfortunately */
int getvalue(struct myobj *o) { return o->value; }
#define VALUE(o) getvalue(o)
void dostuff(struct myobj *foo) {
printf("The value of foo is %d.\n", VALUE(foo)); /* OK */
VALUE(foo) = 1; /* We want a compile error here */
foo->value = 1; /* This is ok. */
}
Ok, I came up with one:
#define VALUE(o) (1 ? (o)->value : 0)
If the variable is always numeric, this works:
#define VALUE(x) (x+0)
or in the context of your example,
#define VALUE(x) (x->value+0)
See §6.5.17 in the C standard (C99 & C1x): “A comma operator does not yield an lvalue.”
#define VALUE(x) (0, x)
(Not portable to C++.)
Try
#define VALUE(o) (const int)((o)->value)
Is this a puzzle or is it an engineering task?
If it's an engineering task, then there are better ways to get opacity of structures in C. In this blog article, I wrote a decent enough description of how to do that in C.

Resources