www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Windows parameter

reply "shuji" <cravstar hotmail.com> writes:
Can someone help me with this:
I have a library which I try to import from D
//funcs.lib
//windows includes...
HWND hwnd;
int setHWND(HWND extHwnd){
	hwnd = extHwnd;
	return 0;
}

//main.d
pragma(lib, "gdi32.lib");
import core.runtime;
import core.sys.windows.windows;
extern (C++) {
	int setHWND(HWND hwnd);
}
int main(int argc, char **argv) {
//windows initializers
	setHWND(hwnd);
	return 0;
}

but when I try to import that function this comes:
    Error 42: Symbol Undefined ?setHWND  YAHPAX Z (int cdecl
setHWND(void *))
--- errorlevel 1

it seems like I cannot use HWND directly.
Jun 30 2013
parent reply "Simen Kjaeraas" <simen.kjaras gmail.com> writes:
On Sun, 30 Jun 2013 20:24:17 +0200, shuji <cravstar hotmail.com> wrote:


 int setHWND(HWND extHwnd){
 	hwnd = extHwnd;
 	return 0;
 }
And:
 extern (C++) {
 	int setHWND(HWND hwnd);
 }
See how these are different? One of them is an extern (C++) function, the other is a D function. In other words, this should work: //funcs.lib //windows includes... HWND hwnd; extern (C++) { // This line! int setHWND(HWND extHwnd){ hwnd = extHwnd; return 0; } } //main.d pragma(lib, "gdi32.lib"); import core.runtime; import core.sys.windows.windows; extern (C++) { int setHWND(HWND hwnd); } int main(int argc, char **argv) { //windows initializers setHWND(hwnd); return 0; } -- Simen
Jun 30 2013
parent reply "shuji" <cravstar hotmail.com> writes:
this code is in a .cpp file
//funcs.lib
//windows includes...
HWND hwnd;
int setHWND(HWND extHwnd){
	hwnd = extHwnd;
	return 0;
}

So im trying to import from D like this. I dont think I can 
implement this line in C++
extern (C++) {}
Jun 30 2013
parent reply "Anthony Goins" <neontotem gmail.com> writes:
On Sunday, 30 June 2013 at 19:03:13 UTC, shuji wrote:
 this code is in a .cpp file
 //funcs.lib
 //windows includes...
 HWND hwnd;
 int setHWND(HWND extHwnd){
 	hwnd = extHwnd;
 	return 0;
 }

 So im trying to import from D like this. I dont think I can 
 implement this line in C++
 extern (C++) {}
I'm probably the most unqualified person to offer help but are you sure you are linking with funcs.lib (assuming setHWND is defined there)
Jun 30 2013
parent reply "shuji" <cravstar hotmail.com> writes:
On Sunday, 30 June 2013 at 20:04:12 UTC, Anthony Goins wrote:
 On Sunday, 30 June 2013 at 19:03:13 UTC, shuji wrote:
 this code is in a .cpp file
 //funcs.lib
 //windows includes...
 HWND hwnd;
 int setHWND(HWND extHwnd){
 	hwnd = extHwnd;
 	return 0;
 }

 So im trying to import from D like this. I dont think I can 
 implement this line in C++
 extern (C++) {}
I'm probably the most unqualified person to offer help but are you sure you are linking with funcs.lib (assuming setHWND is defined there)
yes, i have called other functions in the same file correctly. PD I can call a function with void* as receiving paremeter: //main.d extern (C++) { int setHWND(void* ehwnd); } //mylib.cpp int setmyHWND(void* exthwnd){ hwnd = (HWND)exthwnd; return 0; } and then cast on C++ to HWND, but I think it would be better to send the parameter correctly as HWND.
Jun 30 2013
parent David <d dav1d.de> writes:
Am 30.06.2013 22:20, schrieb shuji:
 On Sunday, 30 June 2013 at 20:04:12 UTC, Anthony Goins wrote:
 On Sunday, 30 June 2013 at 19:03:13 UTC, shuji wrote:
 this code is in a .cpp file
 //funcs.lib
 //windows includes...
 HWND hwnd;
 int setHWND(HWND extHwnd){
     hwnd = extHwnd;
     return 0;
 }

 So im trying to import from D like this. I dont think I can implement
 this line in C++
 extern (C++) {}
I'm probably the most unqualified person to offer help but are you sure you are linking with funcs.lib (assuming setHWND is defined there)
yes, i have called other functions in the same file correctly. PD I can call a function with void* as receiving paremeter: //main.d extern (C++) { int setHWND(void* ehwnd); } //mylib.cpp int setmyHWND(void* exthwnd){ hwnd = (HWND)exthwnd; return 0; } and then cast on C++ to HWND, but I think it would be better to send the parameter correctly as HWND.
make sure the HWND parameter is on both "sides" the same (it has to get mangled correctly!). I don't know if HWND is an opaque struct, if it is, this might get you into trouble. --- Cut here, I looked into core.sys.windows.windows: --- alias void *HANDLE; alias HANDLE HWND; --- That is the problem, on the C++-Side HWND is not of type void* hence it gets mangled differently, you need to get DMD mangle it the same as the C++ compiler does... The problem is (I just looked it up) HWND is *basically* void* All typedefs: PVOID = void* HANDLE = PVOID HWND = HANDLE So you have to "emulate" a typedef to get the correct mangling... Maybe start messing arround with structs? struct HWND; etc. Maybe you could also try to use the deprecated "typedef" in D. Neither of these soloutions are really great. I would consider this another bug on the extern(C++) list.
Jun 30 2013