Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9063815
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T16:05:53+00:00 2026-06-16T16:05:53+00:00

This question is specific to Visual C++ (you may assume Visual C++ 2005 and

  • 0

This question is specific to Visual C++ (you may assume Visual C++ 2005 and later).

I would like to create glue code for a program from unixoid systems (FreeBSD in particular) in order build and run on Win32 with a minimum of changes to the original .c file. Now, most of this is straightforward, but now I ran into an issue. I am using the tchar.h header and TCHAR/_TCHAR and need to create glue code for the err and errx calls (see err(3)) in the original code. Bear with me, even if you don’t agree that code using tchar.h should still be written.

The calls to err and errx take roughly two forms, and this is where the problem occurs:

err(1, "some string with %d format specifiers and %s such", ...)
/* or */
err(1, NULL, ...)

The latter would output the error stored in errno (using strerror).

Now, the question is, is there any way to write a generic macro that can take both NULL and a string literal? I do not have to (and will not) care about variables getting passed as the second parameter, only NULL and literal strings.

Of course my naive initial approach didn’t account for fmt passed as NULL (using variadic macros!):

#define err(eval, fmt, ...) my_err(eval, _T(fmt), __VA_ARGS__)

Right now I don’t have any ideas how to achieve this with macros, because it would require a kind of mix of compile-time and runtime conditionals that I cannot imagine at the moment. So I am looking for an authoritative answer whether this is conceivable at all or not.

The method I am resorting to right now – lacking a better approach – is to write a wrapper function that accepts, just like err and errx, a (const) char * and then converting that to wchar_t * if compiled with _UNICODE defined. This should work, assuming that the caller passes a _TCHAR* string for the variable arguments after the fmt (which is a sane assumption in my context). Otherwise I’d also have to convert %s to %hs inside the format string, to handle “ANSI” strings, as MS calls them.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T16:05:54+00:00Added an answer on June 16, 2026 at 4:05 pm

    Here’s one solution:

    #define _WIN32_WINNT 0x0502
    
    #include <windows.h>
    #include <stdio.h>
    #include <tchar.h>
    
    #ifdef _UNICODE
    
    #define LNULL NULL
    #define fn(x) myfn(L ## x)
    
    #else
    
    #define fn(x) myfn(x)
    
    #endif
    
    void myfn(TCHAR * str)
    {
        if (str == NULL) _tprintf(_T("<NULL>")); else _tprintf(str);
        _tprintf(_T("\n"));
    }
    
    int main(int argc, char ** argv)
    {
        fn("hello");
        fn(NULL);
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have this specific question to do to you, i have a database from
This question may seem a little bit stackoverflow-implementation specific, but I have seen a
This question is specific to reactive-banana and real-time simulations with a physical and visual
This question is specific to the iMacros suite in particular. What is the best
Sorry since this question is specific to my problem. While learning reflections i did
(This question does not rely on a specific IoC framework, so the interfaces and
This question is kind of related to another question but I have a specific
This question is a duplicate of 1042830 , but MonoTouch-specific. Is there a way
I'm reviving this question, and making it more specific: Is there a .NET framework
I noticed This question , but my question is a bit more specific. Is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.