PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

including gx.h in a .c file...

hi,

i'm sure that there is a simple solution to this problem (pocketquake obviously had to solve this). the problem is that gx.h can not be included in a .c file without compiling errors. dan east suggested to modify gx.h but this helps only for the compilation step.

next i get linking errors since gx.lib uses c++ name mangling.
so what i do is, that i define the necessary structures and declare the gapi function:

Code:

struct GXKeyList {
short vkUp;
POINT ptUp;
short vkDown;
POINT ptDown;
short vkLeft;
POINT ptLeft;
short vkRight;
POINT ptRight;
short vkA;
POINT ptA;
short vkB;
POINT ptB;
short vkC;
POINT ptC;
short vkStart;
POINT ptStart;
};

__declspec(dllimport) struct GXKeyList GXGetDefaultKeys(int iOptions);


which produces the following error:

error LNK2019: unresolved external symbol __imp_GXGetDefaultKeys referenced in function wince_GetDefaultKeys

i also tried modifying the gx.h file which ofcourse produces the same results...

any ideas on this?
(since this should work, i assume that i did something completely stupid and apologize beforehand...)

thx,
Daniel
angedelamort · 16 Mar 2004, 20:19 UTC
the only thing I can think that can help you is this site:
http://www.parashift.com/c++-faq-lite/mixing-c-and-cpp.html

the problem is you cannot just change the header... if gapi lib is compiled in c++, you will need to tell the linker where to find the mangled functions.

Read the faq, I'm sure you will find a better way to do it :)
PQ dynamically loads gx.dll, so it doesn't even link to gx.lib.

I would recommend going that route, if only so you can properly inform the user if gx.dll cannot be found.

Dan East
drgoldie wrote:hi,

i'm sure that there is a simple solution to this problem (pocketquake obviously had to solve this). the problem is that gx.h can not be included in a .c file without compiling errors. dan east suggested to modify gx.h but this helps only for the compilation step.

next i get linking errors since gx.lib uses c++ name mangling.
so what i do is, that i define the necessary structures and declare the gapi function:

Code:

struct GXKeyList {
short vkUp;
POINT ptUp;
short vkDown;
POINT ptDown;
short vkLeft;
POINT ptLeft;
short vkRight;
POINT ptRight;
short vkA;
POINT ptA;
short vkB;
POINT ptB;
short vkC;
POINT ptC;
short vkStart;
POINT ptStart;
};

__declspec(dllimport) struct GXKeyList GXGetDefaultKeys(int iOptions);


which produces the following error:

error LNK2019: unresolved external symbol __imp_GXGetDefaultKeys referenced in function wince_GetDefaultKeys

i also tried modifying the gx.h file which ofcourse produces the same results...

any ideas on this?
(since this should work, i assume that i did something completely stupid and apologize beforehand...)

thx,
Daniel

Hi!

I do not really see what is it good for, but in case of dll-s for all functions/symbols there exists a double word (32 bit) in the .lib that is needed to access the real function/symbol in the .dll file.
It is called __imp__<symbolname>
So whenever you want to call a function from a dll. It calls a function referenced at this dd __imp__<symbolname>.
(I think it is because of relocation, (this case relocation happens only at one place))

If you want to simulate it, just link a simple asm file having this dd __imp__ and pointing into the original symbol.


maybe the dllimport makes this, and because of the symbol naming convention difference between C and C++ the linker does not find it.

G.
What you can do of course is use external C linkage! :D

Create a .cpp and a matching .h file in your project and use this example:

Code:

#include "gx.h"
#include "gapi_c.h" //This will be whatever you called your .h file.

extern "C" int GXOPENDISPLAY(HWND hWnd, DWORD dwFlags)
{
return GXOpenDisplay(hWnd,dwFlags);
}

extern "C" int GXCLOSEDISPLAY()
{
return GXCloseDisplay();
}

//Do this for all GAPI functions, i've given you an example to get started with.


In the header, you just prototype the functions as normal:

Example:

Code:

#ifndef GAPI_H
#define GAPI_H

int GXOPENDISPLAY(HWND hWnd, DWORD dwFlags);
int GXCLOSEDISPLAY();
void * GXBEGINDRAW();
int GXENDDRAW();
struct GXDisplayProperties GXGETDISPLAYPROPERTIES();
int GXSUSPEND();
int GXRESUME();

#endif


Then, where you need to call gapi functions, you call the externally linked functions in your cpp file, rather than the functiond in the gapi dll directly. Also, instead of including gx.h, include gapi_c.h (or whatever you called the file.)

Here's a quick example:

Instead of:
Code:

#include "gx.h"

bool GetFrameBuffer()
{
g_fb = (unsigned short*) GXBeginDraw();

if(!g_fb)
return false;

return true;
}


Use:
Code:

#include "gapi_c.h"

bool GetFrameBuffer()
{
g_fb = (unsigned short*) GXBEGINDRAW();

if(!g_fb)
return false;

return true;
}



Hope this helps!
Create a .cpp and a matching .h file in your project

that's what i'm doing at the moment, but this is awfull. this is a c-only project and now due to the windows-ce port i have to add a cpp file...

Read the faq, I'm sure you will find a better way to do it :)

that FAQ tells nothing i did not know before. all it tells is how to get C++ call C code or how to write your C++ code to be callable from C. that's not the problem here...

PQ dynamically loads gx.dll, so it doesn't even link to gx.lib.

thanks for that suggestion. i think i'll go that way...

thanks for all the help,
Daniel
drgoldie wrote:
Create a .cpp and a matching .h file in your project

that's what i'm doing at the moment, but this is awfull. this is a c-only project and now due to the windows-ce port i have to add a cpp file...

You cannot call C++ code from C code directly unless C++ was specifically written to be callable from C. Whoever wrote gx.dll did not follow Win32 calling conventions so gx.dll can only be used by C++ applications.
You can load DLL directly and use GetProcAddress either by name or ordinal if you do not want to add C++ files to you project. Of course, if gx.dll happens to be different on the target system then you're screwed.
kaboomie wrote:
You can load DLL directly and use GetProcAddress either by name or ordinal if you do not want to add C++ files to you project. Of course, if gx.dll happens to be different on the target system then you're screwed.


Assuming you aren't using ordinals, then how would a person dynamically loading the DLL be screwed any worse than a person who statically linked? If the dll happens to be "different" (exported function names differ) then everyone is universally screwed.

Dan East
Dan East wrote:
Assuming you aren't using ordinals, then how would a person dynamically loading the DLL be screwed any worse than a person who statically linked? If the dll happens to be "different" (exported function names differ) then everyone is universally screwed.


OK, I take it back. Everyone will be universally screwed.
just to close the issue: loading dynamically works great. i also learned something cool from the freeglut guys:

there's no need to create your own gx.h file. all that's needed is to typedef the structures

Code:
typedef struct GXDisplayProperties GXDisplayProperties;
typedef struct GXKeyList GXKeyList;


before including gx.h. i didn't know that one can typedef something to the same name but it works fine.

bye,
Daniel