define debug using Unicode Character Set - windows

I'm following a book called "Introduction to 3D Game Programming with DirectX 9.0c: A Shader Approach" and all the example there are using Multi-Byte Character Set and I don't want to use it and I don't want my project to be in multi-bye chacters. My problem is there is a debug function on the book here is the code.
//debug
#if defined(DEBUG) | defined(_DEBUG)
#ifndef HR
#define HR(x) \
{ \
HRESULT hr = x; \
if(FAILED(hr)) \
{ \
DXTrace(__FILE__, __LINE__, hr, #x, TRUE); \
} \
}
#endif
#else
#ifndef HR
#define HR(x) x;
#endif
#endif
then on my .cpp files i used this code on the book to create device.
HR(md3dObject->CreateDevice(
D3DADAPTER_DEFAULT, // primary adapter
mDevType, // device type
mhMainWnd, // window associated with device
devBehaviorFlags, // vertex processing
&md3dPP, // present parameters
&gd3dDevice)); // return created device
then the error is. error C2664: 'DXTraceW' : cannot convert parameter 4 from 'const char [107]' to 'const WCHAR *'
hope someone can help me. thx.

Try changing the parameter from #x to L#x, or use Microsoft's macro _T(#x).

Related

Multiple definition error while attempting to edit bootloader of SAMD51

I am currently working on adding some new functionality to a SAMD51 bootloader. The original bootloader code I am basing this off of is the Adafruit UF2 bootloader for SAMD microcontrollers, found here: https://github.com/adafruit/uf2-samdx1
I am currently grafting in some SD-card functionality using code from ASF4 (Atmel Software Framework 4).
I have a pair of files hal_atomic.h and hal_atomic.c that are defined as such:
hal_atomic.h:
#ifndef _HAL_ATOMIC_H_INCLUDED
#define _HAL_ATOMIC_H_INCLUDED
#include "../utils/compiler.h"
#ifdef __cplusplus
extern "C" {
#endif
typedef uint32_t hal_atomic_t;
#define CRITICAL_SECTION_ENTER() \
{ \
volatile hal_atomic_t __atomic; \
atomic_enter_critical(&__atomic);
#define CRITICAL_SECTION_LEAVE() \
atomic_leave_critical(&__atomic); \
}
void atomic_enter_critical(hal_atomic_t volatile *atomic);
void atomic_leave_critical(hal_atomic_t volatile *atomic);
uint32_t atomic_get_version(void);
#ifdef __cplusplus
}
#endif
#endif /* _HAL_ATOMIC_H_INCLUDED */
hal_atomic.c:
void atomic_enter_critical(hal_atomic_t volatile *atomic)
{
*atomic = __get_PRIMASK();
__disable_irq();
__DMB();
}
void atomic_leave_critical(hal_atomic_t volatile *atomic)
{
__DMB();
__set_PRIMASK(*atomic);
}
uint32_t atomic_get_version(void)
{
return DRIVER_VERSION;
}
When I attempt to compile this code, I am getting some errors. The code references a function in the CMSIS library called __get_PRIMASK'. That function is defined as follows in a file called core_cmFunc.h`:
__attribute__( ( always_inline ) ) __STATIC_INLINE uint32_t __get_PRIMASK(void)
{
uint32_t result;
__ASM volatile ("MRS %0, primask" : "=r" (result) );
return(result);
}
When I try to compile the code, I get the following errors:
/asf4/hal/hal_atomic.o: In function `__get_PRIMASK':
/lib/cmsis/CMSIS/Include/core_cmFunc.h:471: multiple definition of `atomic_enter_critical'
/asf4/hal/hal_atomic.o: /lib/cmsis/CMSIS/Include/core_cmFunc.h:471: first defined here
/asf4/hal/hal_atomic.o: In function `__get_PRIMASK':
/lib/cmsis/CMSIS/Include/core_cmFunc.h:471: multiple definition of `atomic_leave_critical'
/asf4/hal/hal_atomic.o: /lib/cmsis/CMSIS/Include/core_cmFunc.h:471: first defined here
/asf4/hal/hal_atomic.o: In function `__get_PRIMASK':
/lib/cmsis/CMSIS/Include/core_cmFunc.h:471: multiple definition of `atomic_get_version'
/asf4/hal/hal_atomic.o: /lib/cmsis/CMSIS/Include/core_cmFunc.h:471: first defined here
collect2.exe: error: ld returned 1 exit status
I'm not sure why it thinks these functions are defined multiple times. I was wondering if anyone could help me make sense of this? Thank you!!!

How to use __VA_ARGS__ variadic in 8051

I'm trying to build a project 8051 in Keil IDE.
I have a definition to print information for purposes debug program as following:
#define LOGI(fmt, ...) printf("[I] %s:%u: "fmt, __FILE__, __LINE__, ##__VA_ARGS__)
But there are errors:
log.h(18): error C301: identifier expected
log.h(18): error C301: identifier expected
log.h(18): error C304: bad macro parameter list
Please help me fix this code, thank you.
According to the documentation, Keil C51 is based on C90. So it does not support __VA_ARGS__ that is a C99 addition.
However, you can work around this for example by this trick. Use a parenthesized argument.
#define LOGI(args) \
do { \
printf("[I] %s:%u: ", __FILE__, __LINE__); \
printf args; \
} while (0)
void f(void) {
LOGI(("address of f() = %p\n", f));
}
Another possible solution is to provide an own function with a variable number of arguments, see the example in the documentation. This is a cleaner way because you can use this function without a "hick-up" when reading the source code because of the double parentheses. But be aware that these arguments are not placed in registers, and more memory on stack and in code is used.
#include <stdio.h>
#include <stdarg.h>
void logi(const char* filename, int line, char *fmt, ...) {
va_list arg_ptr;
va_start(arg_ptr, fmt);
printf("[I] %s:%u: ", filename, line);
vprintf(fmt, arg_ptr);
va_end(arg_ptr);
}
void f(void) {
logi(__FILE__, __LINE__, "Hello %u %u", 1 , 2);
}
Note: You might want to switch to another compiler, which supports some newer standard than a 30 years old one.

Visual Studio Syntax Error on offsetof()

I have type:
typedef struct
{
int x;
int y;
int z;
} sdf_test_t;
But when I try to compile the following:
offset = offsetof(sdf_test_t, z);
Visual Studio responds with:
c:\dataflash.c(542) : error C2143: syntax error : missing ')' before 'type'
c:\dataflash.c(542) : error C2059: syntax error : ')'
What is wrong here?
I am using:
Microsoft Visual Studio 2008 x86
Microsoft (R) Visual Studio Version 9.0.21022.8.
The offsetof macro is defined in <stddef.h> as follows:
/* Define offsetof macro */
#ifdef __cplusplus
#ifdef _WIN64
#define offsetof(s,m) (size_t)( (ptrdiff_t)&reinterpret_cast<const volatile char&>((((s *)0)->m)) )
#else
#define offsetof(s,m) (size_t)&reinterpret_cast<const volatile char&>((((s *)0)->m))
#endif
#else
#ifdef _WIN64
#define offsetof(s,m) (size_t)( (ptrdiff_t)&(((s *)0)->m) )
#else
#define offsetof(s,m) (size_t)&(((s *)0)->m)
#endif
#endif /* __cplusplus */
By elimination. I've established that the compiler uses:
#define offsetof(s,m) (size_t)&reinterpret_cast<const volatile char&>((((s *)0)->m))
I've made a simple program as it:
#include <stddef.h>
typedef struct
{
int x;
int y;
int z;
} sdf_test_t;
int main() {
size_t offset = offsetof(sdf_test_t, z);
return 0;
}
I don't have any problems, i think that you can try to isolate the code in another project and test it again.
I managed to fix it by adding the following line to my source file:
#include <stddef.h>
From this, it seems that Visual Studio silently includes header files if you don't include them explicitly. Even worse, It assumes that the source file is C++ by default.
If I don't include a header file with a symbol I use, I would expect the compiler to scream out and report an error, not just make up something...

What to #include for code about windows,raw mouse data

I find the following post very useful to do a project of my own. Here's the newbie question then: what must I include for this to work?
Link:
How to accurately measure mouse movement in inches or centimetres for a mouse with a known DPI
Content:
The following code registers the RAWINPUTDEVICE so it can be used in WM_INPUT.
RAWINPUTDEVICE Rid[1];
Rid[0].usUsagePage = HID_USAGE_PAGE_GENERIC;
Rid[0].usUsage = HID_USAGE_GENERIC_MOUSE;
Rid[0].dwFlags = RIDEV_INPUTSINK;
Rid[0].hwndTarget = hWnd;
RegisterRawInputDevices(Rid, 1, sizeof(Rid[0]);
The following code acutally uses the Rid variable two determine how many pixels the mouse has moved since the last time WM_INPUT was initiated.
case WM_INPUT:
{
UINT dwSize = 40;
static BYTE lpb[40];
GetRawInputData((HRAWINPUT)lParam, RID_INPUT,
lpb, &dwSize, sizeof(RAWINPUTHEADER));
RAWINPUT* raw = (RAWINPUT*)lpb;
if (raw->header.dwType == RIM_TYPEMOUSE)
{
int xPosRelative = raw->data.mouse.lLastX; // Could be 1, or could be more than 1
int yPosRelative = raw->data.mouse.lLastY; // Could be 1, or could be more than 1!
}
break;
}
I just found it.
#include "hidusage.h"
here's some definitions it has
#define HID_USAGE_GENERIC_POINTER ((USAGE) 0x01)
#define HID_USAGE_GENERIC_MOUSE ((USAGE) 0x02)
#define HID_USAGE_GENERIC_JOYSTICK ((USAGE) 0x04)
#define HID_USAGE_GENERIC_GAMEPAD ((USAGE) 0x05)
#define HID_USAGE_GENERIC_KEYBOARD ((USAGE) 0x06)
#define HID_USAGE_GENERIC_KEYPAD ((USAGE) 0x07)
#define HID_USAGE_GENERIC_SYSTEM_CTL ((USAGE) 0x80)
typedef USHORT USAGE,*PUSAGE;
You need to include windows.h
...also the HID_USAGE_PAGE_GENERIC and HID_USAGE_GENERIC_MOUSE must be defined.
See MSDN..
#ifndef HID_USAGE_PAGE_GENERIC
#define HID_USAGE_PAGE_GENERIC ((USHORT) 0x01)
#endif
#ifndef HID_USAGE_GENERIC_MOUSE
#define HID_USAGE_GENERIC_MOUSE ((USHORT) 0x02)
#endif
http://msdn.microsoft.com/en-gb/library/windows/desktop/ee418864%28v=vs.85%29.aspx

MIDL (Constant) References

Are there no constant references in MIDL method declarations????
eg.
[id(1), helpstring("My Method")]
HRESULT MyMethod(
[in] IID & const rclsid
);
for
HRESULT MyMethod(
IID const &rclsid
);
MIDL doesn't really support reference parameters, it only supports "in" and "out" parameters. So if you DO pass in a reference, it's just syntactic sugar for a pointer to the value (the issue is observability - if you have a callback function or interface in our method signature, changes to a reference would be observable from the callback, but changes to an [out] parameter aren't visible until the function returns.
In addition, the difference between "& const" and "const &" are lost. If you look at the definition of REFGUID, you'll see that they only use one form of "const" for C++ code:
#ifdef __midl_proxy
#define __MIDL_CONST
#else
#define __MIDL_CONST const
#endif
#ifndef _REFGUID_DEFINED
#define _REFGUID_DEFINED
#ifdef __cplusplus
#define REFGUID const GUID &
#else
#define REFGUID const GUID * __MIDL_CONST
#endif
#endif

Resources