Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Using Dialog Templates to create an InputBox() in C++

, 15 Mar 2006
Using dialog templates to create an InputBox() in C++, similar to that of VB's InputBox().
#include <windows.h>
#include <stdio.h>

int main(int argc, char *argv[])
  printf("DlgResToDlgTemplate v1.0 (c) <>\n\n");

  if (argc < 4)
    printf("usage: %s MODULE_FILE DLG_ID FILE.H\n", argv[0]);
    return -1;

  int dlgID = atoi(argv[2]);

  HMODULE hModule;
  hModule = ::LoadLibraryEx(argv[1], 0, LOAD_LIBRARY_AS_DATAFILE);
  if (hModule == NULL)
    printf("Could not load module '%s'\n", argv[1]);
    return -1;

  HRSRC hrsrc = ::FindResource(hModule, (LPCTSTR) dlgID, RT_DIALOG);
  if (hrsrc == NULL)
    printf("Cannot find dialog resource with id '%d'\n", dlgID);
    return -2;

  HGLOBAL hglobalRsrc = ::LoadResource(hModule, hrsrc);
  if (hglobalRsrc == NULL)
    printf("Cannot load resource!\n");
    return -3;

  char *buf = (char *)::LockResource(hglobalRsrc);
  if (buf == NULL)
    printf("Cannot lock resource!\n");
    return -4;

  DWORD sz = ::SizeofResource(hModule, hrsrc);

  FILE *fp = fopen(argv[3], "wb");
  if (fp == NULL)
    printf("Could not open '%s' for writing\n", argv[3]);
    return -5;

  char temp[100];

  sprintf(temp, "static unsigned char dlg_%d[] = \n{\n\t", dlgID);
  fwrite(temp, 1, strlen(temp), fp);

  for (DWORD i=0;i<=sz;i++)
    sprintf(temp, "0x%02x", (unsigned char)(buf[i] & 0xFF));
    fwrite(temp, 1, strlen(temp), fp);

    // don't append a comma if it was last byte
    if (i != sz)
      fwrite(", ", 1, 1, fp);

    if (i && (i % 16 == 0))
      fwrite("\n\t", 1, 2, fp);

  strcpy(temp, "\n};\n");
  fwrite(temp, 1, strlen(temp), fp);


  printf("generated %s (%d bytes) successfully!\n", argv[3], sz);

  return 0;

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Elias Bachaalany
Web Developer
United States United States
Elias (aka lallousx86, @0xeb) has always been interested in the making of things and their inner workings.
His computer interests include system programming, reverse engineering, writing libraries, tutorials and articles.
In his free time, and apart from researching, his favorite reading topics include: dreams, metaphysics, philosophy, psychology and any other human/mystical science.
Former employee of Hex-Rays (the creators of IDA Pro), was responsible about many debugger plugins, IDAPython project ownership and what not.
Elias currently works at Microsoft as a software security engineer.
More articles and blog posts can be found here:

| Advertise | Privacy | Mobile
Web02 | 2.8.140827.1 | Last Updated 16 Mar 2006
Article Copyright 2006 by Elias Bachaalany
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid