Win32 SDK question - CreateDialog fails - HELP !

I am porting a 16-bit DLL to the 32-bit world. The exe which uses this DLL is also being ported. In the DLL there is a function which calls CreateDialog to create a modeless dialog box. The code in the 16 bit world looked like this :

Begin DLL exported function ()
hwnd = NULL;
if (hwnd == NULL)
 hwnd = CreateDialog (hinst,                                MAKEINTRESOURCE (cancel_dialog),

_itoa( TaskInfo.hTask,buffer,8 );
SetDlgItemText(hwnd, Task_ID, buffer);

_itoa( (int) CallArg.thread_handle,buffer,8 );
SetDlgItemText(hwnd, Thread_ID, buffer);

SetWindowLong (hwnd, DWL_USER, (LONG) &CallArg);
MessageBox(NULL,"entering new","Info",MB_OK);

EndDialog (hwnd, 0);
} // end of DLL exported fn.

In the 32 bit world I have changed the 3rd parameter to hParent which is passed from the EXE as the EXE's hWnd.

When I run this in my Win NT 4.0 Workstation the CreateDialog procedure does not create any dialog box on the screen. According to docs, when CreateDialog fails it returns hwnd as NULL and you should be able to see GetLastError to find out which error has occured.

However, though hwnd is returned as NULL, there does'nt seem to be any error set as GetLastError returns 0 !! I've racked my brains out for hours on this - any suggestions ?

Who is Participating?

Improve company productivity with a Business Account.Sign Up

Answers2000Connect With a Mentor Commented:
Okay here's the answer:
1. Bridge's tip confirms the Resources are not being linked into the 32 bit DLL

2. In Win16 look in the make file.  You will probably find a two stage compile of the resources
i. rc -r or rc /r will compile the .rc into a .res
ii. a second call to rc which links the .res into the DLL

3. In Win32 .res files are linked to your DLL by the linker (link).  
i. Remove 2.ii
ii. Add .res to the link (this will either be in the main make file or a file referenced from link's command line)
iii. Add the .res to the link option (in the make file) dependency list (this is stuff to the right of the colon)

Once you've recompiled everything with the make file, repeat loading the DLL into VC.  You should now see the resources.

Now test the DLL.  If it doesn't check the initialization code in DllMain and that hInst is being setup correctly.  You should be able to confirm this by setting hInst to NULL when you declare it, and adding a test before CreateDialog to check it's

If you're still having trouble or I haven't explained well enough, reject this, and post the make file as part of you're comment (or at least the bits around calls to rc and link).

I think the problem here is the resource template of this dialog. Where does it reside? Probably because it is in the .DLL the CreateDialog function does not find the resource because it looks in the .EXE resources. Try to see if FindResource function works. If it works then the problem is somewhere else :)

What does this have to do with MFC?  Have you translated this to a straight SDK program, or an MFC program?

What controls are on the dialog template?

What styles does the dialog template carry?

How is cancel_dialog defined?

How was hinst initialized?

If you open the DLL in the IDE, is the resource apparent?

B ekiM
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

The clue is that it worked in Win16 and doesn't in Win32.

The answer is therefore
1. The hInst should be the hInst of the DLL
2. The CreateDialog call works in Win16 because in this case the hInst is correctly initialized, and fails in Win32 because in this case it isn't.
3. I bet the code includes a LibMain function (Win16 library start up point) which initializes hInst.  Under Win32 this function will never be called, hence hInst has the wrong value in this case.
4. Solution
i. Find LibMain, remove it (and cut out an initialization code)
ii. Delete LibMain
iii. Add DllMain, and paste the initialization code in here (including setting up the hInst)
fstabAuthor Commented:
Thank you all for responding! I shall try to answer your questions first -

1. This is a straight SDK program which was in Win16
2. Yes I did change LibMain to DllMain - I mean before I ran into this problem I had done all the necessary "ports"
3. Here is the dialog template :

#define cancel_dialog 1000
#define Thread_ID 100
#define Task_ID   101

cancel_dialog DIALOG DISCARDABLE  8, 17, 160, 75
CAPTION "Greet DLL - Status"
FONT 8, "MS Sans Serif"
    PUSHBUTTON      "Cancel",IDCANCEL,54,41,52,27
    RTEXT           "thread id = ",-1,43,22,39,8
    EDITTEXT        Thread_ID,85,22,28,12,ES_READONLY | NOT WS_BORDER | NOT
    RTEXT           "task id = ",-1,47,11,35,8
    EDITTEXT        Task_ID,85,11,28,12,ES_READONLY | NOT WS_BORDER | NOT

4. Mikeblas - when I open the DLL in the IDE the resource is *NOT* apparent ! But the resources are not apparent when I open any other EXE or DLL in the IDE - VC++ 5.0.

5. bobby - I have'nt tried FindResource yet so watch this space ..

open the DLL or EXE as resourse ( option near the bottom of the Open Dialog ) to view it.
Remove the .RC from the project, and re-add it.  Somehow it seems to have become separated.

Bridge's tip should confirm if this is the case (do this before my change)
fstabAuthor Commented:
I did open the dll as a resource but it shows only the DLL as a folder icon - no tree expanding to show the resources.

Also Answers2000 - this is not a VC project/workspace. All the action takes place through a Makefile which is used by nmake.

fstabAuthor Commented:
And Answers is the winner ...

Yes Answers - the problem was the .RES was'nt getting linked to the DLL object files while producing the DLL !

Thanks to everybody who did the brain racking .. unfortunately I can't distribute points :-) !!

If the resources are not evident when you open an EXE or DLL in the IDE, then you're not correctly using the IDE.

B ekiM

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.