Having trouble dynamically creating a new CDialog object.

[b][red]This message was edited by Flack at 2003-4-25 12:30:15[/red][/b][hr]

I have a MFC dialog app that I created with VC++ AppWizard.
Now, from my original dialog box, I want to create a new one, which appears as a separate popup. However, the code I have does not work (probably because it's totally wrong).

Anyway, here is the code (this code is in a function I call when a button is pushed):
myTemplate.dwExtendedStyle = NULL,
myTemplate.cdit = 0;
myTemplate.x = 10;
myTemplate.y = 10;
myTemplate.cx = 100;
myTemplate.cy = 100;

int nBufferSize = sizeof(DLGTEMPLATE);
HLOCAL hLocal = LocalAlloc(LHND, nBufferSize);
BYTE* pBuffer = (BYTE*)LocalLock(hLocal);
BYTE* pdest = pBuffer;

// transfer DLGTEMPLATE structure to the buffer
memcpy(pdest, &myTemplate, sizeof(DLGTEMPLATE));

CDialog myDialog;


Now, the InitModalIndirect returns a nonzero value,indicating that the dialog object was created and initialized successfully. However, the line myDialog.DoModal()(red line) returns a -1 indicating failure. Does anyone know what I am doing wrong?

My goal is to eventually have a separate dialog popup that contains a list box but I can't even get an empty dialog window to appear.

Thanx for the help,


  • the key error is located in this function
    the first argument of this function should be the type of HGlOBAL(a handle),but you put a pointer there,So it should be like this:

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!


In this Discussion