Jump to content
The simFlight Network Forums

Recommended Posts

Posted

Hi,

I dismissed the C# for now and I am trying to create the module using C++.

The problem is that when I do a C project, everything runs fine. But when I try to change it to C++ project, the DLL compiles, but in the flight simulator is doesnt show the menu bar any more :( ..

I am using the normal example that everybody knows :) :

/* Replace "dll.h" with the name of your header */

#include "dll.h"

#include

#include "module.h"

DllClass::DllClass()

{

}

DllClass::~DllClass ()

{

}

/*

* We define just basic interface to the flight simulator so it will be

* able to load the module.

*/

DLLEXPORT MODULE_IMPORT ImportTable = {

{0x00000000, NULL},

{0x00000000, NULL}

};

void FSAPI module_init(void) {}

void FSAPI module_deinit(void) {}

DLLEXPORT MODULE_LINKAGE Linkage = {

0x00000000,

module_init,

module_deinit,

0,

0,

0x0900, // FS2004 version (use 0x0800 for FS2002)

NULL

};

// The standard window procedure used by the flight simulator

WNDPROC oldWndProc;

// Flight simulator main window handle

HWND hFSimWindow;

#define MENU_ENTRY "My Mo&dule"

#define ID_MY_MENUITEM 40001

/**

* Main window procedure that is called by the flight simulator to process

* incoming window messages.

*/

LRESULT CALLBACK FSimWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)

{

switch (uMsg) {

case WM_NCPAINT:

{

HMENU hMenu, hMyMenu;

hMenu = GetMenu(hwnd);

if (hMenu != NULL) {

int i;

// Look for our menu entry in the main menu.

for (i = 0; i < GetMenuItemCount(hMenu); i++) {

char buf[128];

GetMenuString(hMenu, i, buf, 128, MF_BYPOSITION);

if (strcmp(buf, MENU_ENTRY) == 0) {

// It is already here, we do not need to add it again

break;

}

}

if (i < GetMenuItemCount(hMenu)) {

// It is already here, we do not need to add it again

break;

}

/* Create new menu. NOTE: It seems that this will be

* reached more times, so we cannot save the handle, because

* in such case it could be destroyed and we will not have

* any access to it in the simulator.

*/

hMyMenu = CreateMenu();

AppendMenu(hMyMenu, MF_STRING | MF_ENABLED, ID_MY_MENUITEM, "My &First Menu Entry");

// add the created menu to the main menu

AppendMenu(hMenu, MF_STRING | MF_POPUP, (UINT_PTR)hMyMenu, MENU_ENTRY);

}

}

break;

case WM_COMMAND:

if (LOWORD(wParam) == ID_MY_MENUITEM) {

// Add your code here

HINSTANCE aux = NULL;

MessageBox(hwnd, "ola", "HURA", MB_OK | MB_ICONEXCLAMATION);

return 0;

}

break;

}

// Call the original window procedure to handle all other messages

return CallWindowProc(oldWndProc, hwnd, uMsg, wParam, lParam);

}

BOOL APIENTRY DllMain (HINSTANCE hInst /* Library instance handle. */ ,

DWORD reason /* Reason this function is being called. */ ,

LPVOID reserved /* Not used. */ )

{

switch (reason)

{

case DLL_PROCESS_ATTACH:

hFSimWindow = FindWindow("FS98MAIN", NULL);

oldWndProc = (WNDPROC)SetWindowLong(hFSimWindow, GWL_WNDPROC, (LONG)FSimWindowProc);

break;

case DLL_PROCESS_DETACH:

break;

case DLL_THREAD_ATTACH:

break;

case DLL_THREAD_DETACH:

break;

}

/* Returns TRUE on success, FALSE on failure */

return TRUE;

}

Please can you see what I am doing wrong ?

Thank you !! ..

Posted

The problem is that when I do a C project, everything runs fine. But when I try to change it to C++ project, the DLL compiles, but in the flight simulator is doesnt show the menu bar any more :( ..

I doubt if the module is loadingdid you check? The export names for C++ use some weird mangling which will disguise the two needed link address names. I only use C, but you might get away with defining the exported procedures as C even if the rest isn't.

Regards,

Pete

Posted

Try placing an extern "C" in front of the ImportTable and Linkage definitions; ie:

extern "C" DLLEXPORT MODULE_IMPORT ImportTable = {

and

extern "C" DLLEXPORT MODULE_LINKAGE Linkage = {

As Pete says, C++ tends to mangle the exported names of stuff compared to straight C.

If that doesn't work, you could move the C++ code to a seperate .CPP file and leave the straight C code required to interface to FS in a .C file.

Also, I noticed you mentioned C# at the top of your post, make sure you are using Native C++ and not Managed C++, FS won't load a Managed C++ module.

Tim

Posted

Hi,

First of all thank you for you help.

Pete the code was not 100% that one. It's injected on a C++ project so you might see some things that only appear on this project.

Beatle, unfortunately it didnt work.

Also, I made a debug and I saw that the FS enters inside the "DLLMain" but somehow it just does that. It doesnt call the FSimWindowProc function.

About the Native C++ code and not managed, this is the command arguments that Visual C++ is generating:

/Od /GL /D "WIN32" /D "_DEBUG" /D "_WINDOWS" /D "_USRDLL" /D "TESTE27_EXPORTS" /D "_WINDLL" /FD /EHsc /MTd /GS /Yu"stdafx.h" /Fp"Debug/teste27.pch" /Fo"Debug/" /Fd"Debug/vc70.pdb" /FR"Debug/" /W3 /nologo /c /Wp64 /Zi /TP

Also, the option "Use Managed Extensions" is "OFF". Is this correct ?

(Thank you)

Posted

Also, the option "Use Managed Extensions" is "OFF". Is this correct ?

I don't know what all the symbols mean (never needed to find out -- I only use the Project Settings), but the fact that you are even offered "managed extensions" seems to me to indicate that you may, indeed, be compiling your module as managed (ugh, means "interpreted" to me) code!

Regards,

Pete

Posted
Hi,

Also, I made a debug and I saw that the FS enters inside the "DLLMain" but somehow it just does that. It doesnt call the FSimWindowProc function.

Also, the option "Use Managed Extensions" is "OFF". Is this correct ?

DLLMain is called by the OS DLL loader when the DLL is loaded, not by FSim. You might want to try adding some dummy code inside the module_init function and place a breakpoint in there and see if the debugger gets there, that will give you a better idea if FS is really loading your DLL correctly inside its process space.

Haven't used C++ with the latest VStudio IDE, but the Use Managed Extensions OFF sounds right (Pete, the new IDE handles both managed and "normal" C++ in the same IDE now, so I imagine that option just chooses between them) - also the /D WIN32 and /D _WINDLL command line options seem to imply a standard Win32 PE DLL, so I think you are good there.

Tim

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use. Guidelines Privacy Policy We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.