www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Can I use resource file(*.rc,*.res) in D under Win32 API?

reply Sam Hu <samhudotsamhu gmail.com> writes:
Greetings to everybody!

As subjected.I use vc++ to generate a very simple dialog resource,and then
compiled with rcc.exe accompanied with dmc:
rcc -32 mydialog.rc

it generated mydialog.res file.

In my main win32 SDK source file,I use DialogBox to try to load the dialog,but
it failed.

Doesn't  this make sense?Any solutions?
Thanks for your help in advance.

Regards,
Sam
Sep 11 2009
parent reply Stewart Gordon <smjg_1998 yahoo.com> writes:
Sam Hu wrote:
 Greetings to everybody!
 
 As subjected.I use vc++ to generate a very simple dialog resource,and then
compiled with rcc.exe accompanied with dmc:
 rcc -32 mydialog.rc
 
 it generated mydialog.res file.
Did you remember to include this .res file on the command line when linking your program? Seeing what command lines you're using might help.
 In my main win32 SDK source file,I use DialogBox to try to load the dialog,but
it failed.
<snip> By "failed", do you mean that the function returned an error value but otherwise did nothing? Stewart.
Sep 11 2009
parent reply Sam Hu <samhudotsamhu gmail.com> writes:
Thank you so much for your help!

Below is the resource file,I compiled it with 

rcc -32 testd.rc

it then generated 
testd.res.

testd.rc:

#include <windows.h>


101 DIALOG 129, 26, 180, 70
STYLE WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_SYSMENU
CAPTION "Show Info"
FONT 8, "MS Sans Serif"
BEGIN
    EDITTEXT        102, 19, 15, 147, 12, ES_AUTOHSCROLL
    PUSHBUTTON      "&Quit", IDCANCEL, 107, 42, 40, 14
    PUSHBUTTON      "&OK", IDOK, 28, 43, 40, 14
END


Below is the D source file:
module testd;

//import std.c.windows.windows;
import win32.windows;
import std.string;
import std.conv;

import std.stdio;

import core.runtime;
import core.stdc.stdlib;
import core.stdc.stdio;

//extern(Windows)
//int MessageBoxW(HWND handle,in wchar* text,in wchar* caption,int style);
extern(Windows)
int WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int
nCmdShow)
{
    
    try
    {
	
		Runtime.initialize;
		runModuleUnitTests;
	
		WNDCLASSEX wc;
		//memset(&wc,0,wc.sizeof);
		wc.cbSize=wc.sizeof;
		wc.lpfnWndProc=&DefDlgProc;
		wc.cbWndExtra=DLGWINDOWEXTRA;
		wc.hInstance=hInstance;
		wc.hCursor=LoadCursor(null,IDC_ARROW);
		wc.hbrBackground=cast(HBRUSH)(COLOR_WINDOW+1);
		wc.lpszClassName=toStringz("dialog");
		if(!RegisterClassEx(&wc))
		{
			MessageBox(null,toStringz("Register class
failed"),toStringz("failed"),MB_OK|MB_ICONERROR);
			return -1;
		}
	
	
		if(!CreateDialog/*DialogBox*/(hInstance, 
                     MAKEINTRESOURCE(101), 
                     null, cast(DLGPROC)&DialogFunc))
		{
			MessageBox(null,toStringz("Create Dialog
failed!"),toStringz("failed"),MB_OK|MB_ICONINFORMATION);
			return -1;
		}
		return 0;
    }

    catch (Object o)		// catch any uncaught exceptions
    {
		MessageBoxW(HWND.init, cast(wchar*)o.toString, "Fatal Error",
		    MB_OK | MB_ICONEXCLAMATION);
	
	
		return -1;		// failed
	
    }
    finally
    {
		
    }

	// run finalizers; terminate garbage collector
    Runtime.terminate;
    		
    return 0;
}



int  DialogFunc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam)
{
	switch(msg)
	{
		case WM_COMMAND:
		switch(LOWORD(wParam))
		{
			case IDOK:
			EndDialog(hwnd,1);
			return 1;
			case IDCANCEL:
			EndDialog(hwnd,0);
			return 1;
		}
		break;
		case WM_CLOSE:
			EndDialog(hwnd,0);
			return 1;
	}
	return 0;
}

I compiled with 
bud -O -release -gui -clean testd.d testd.res and it generated testd.exe.
When I run testd.exe,first I heard a beep,then windows popped up an error
messagebox:
testd.exe has encountered a problem and needs to close.We are sorry for the
inconvenience.

I tried to upload the screen shot but failed.

So where should I start from?

Regards,
Sam
Sep 14 2009
next sibling parent reply Don <nospam nospam.com> writes:
Sam Hu wrote:
 int  DialogFunc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam)
This should be extern(Windows). I'm surprised that compiled, it shouldn't have.
Sep 14 2009
parent reply Sam Hu <samhudotsamhu gmail.com> writes:
Don Wrote:

 Sam Hu wrote:
 int  DialogFunc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam)
This should be extern(Windows). I'm surprised that compiled, it shouldn't have.
Yes.it compiled even without extern(Windows) ,maybe because I imported win32 package other than std.c.windows.windows. After added extern(Windows) to it,all the same result. Oops!
Sep 14 2009
parent reply Stewart Gordon <smjg_1998 yahoo.com> writes:
Sam Hu wrote:
 Don Wrote:
 
 Sam Hu wrote:
 int  DialogFunc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam)
This should be extern(Windows). I'm surprised that compiled, it shouldn't have.
Yes.it compiled even without extern(Windows) ,maybe because I imported win32 package other than std.c.windows.windows.
<snip> No, it's because you put a cast in to suppress the error. Stewart.
Sep 14 2009
parent Sam Hu <samhudotsamhu gmail.com> writes:
Stewart Gordon Wrote:
 No, it's because you put a cast in to suppress the error.
 
 Stewart.
You are right.It is the switch statement.I followed your guide and finally make it.Great! Thank you so much!
Sep 15 2009
prev sibling parent reply Stewart Gordon <smjg_1998 yahoo.com> writes:
Sam Hu wrote:
<snip>
 int  DialogFunc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam)
 {
 	switch(msg)
 	{
 		case WM_COMMAND:
 		switch(LOWORD(wParam))
 		{
 			case IDOK:
 			EndDialog(hwnd,1);
 			return 1;
 			case IDCANCEL:
 			EndDialog(hwnd,0);
 			return 1;
 		}
 		break;
 		case WM_CLOSE:
 			EndDialog(hwnd,0);
 			return 1;
 	}
<snip> Your problem is right here: In D, switch statements must have default clauses of there's any chance of the value being switched not matching any of the switches. If there isn't, a SwitchError is thrown at runtime. Presumably, the program crashes because there's nothing catching the error before Windows does. The same applies in the inner switch. Stewart.
Sep 14 2009
parent Sam Hu <samhudotsamhu gmail.com> writes:
Stewart Gordon Wrote:
 Your problem is right here: In D, switch statements must have default 
 clauses of there's any chance of the value being switched not matching 
 any of the switches.  If there isn't, a SwitchError is thrown at 
 runtime.  Presumably, the program crashes because there's nothing 
 catching the error before Windows does.
 
 The same applies in the inner switch.
 
 Stewart.
Thank you so much for all your help! I make it finally.Now looking back found it kind of easy,no much difference with VC++.:P Regards, Sam
Sep 15 2009