cannot convert 'modentry32*' to 'tagMODULEENTRY32*'

Not sure what im doing wrong but im getting the error below when i try to use Module32First() function.

1
2
3
4
5
6
7
8
9
10
11
12
struct modentry32{
    DWORD   dwSize;
    DWORD   th32ModuleID;
    DWORD   th32ProcessID;
    DWORD   GlblcntUsage;
    DWORD   ProccntUsage;
    BYTE    *modBaseAddr;
    DWORD   modBaseSize;
    HMODULE hModule;
    TCHAR   szModule[MAX_MODULE_NAME32 + 1];
    TCHAR   szExePath[MAX_PATH];
};modentry32 *mod32;


Module32First(snapshot,mod32);

error: cannot convert 'modentry32*' to 'tagMODULEENTRY32*' for argument '2' to 'BOOL Module32First(void*, tagMODULEENTRY32*)'
You should define a MODULEENTRY32 object instead of your "modentry32 mod32"
Luckily, I'm writing a task manager project. So I recommend you use regular definition type instead of 'pointer'. (Your definition may cause 100% crash, for certain)

HTH
Where is modentry32 defined?

The Windows SDK header TlHelp32.h declares Module32First as

1
2
3
4
BOOL WINAPI Module32First(
  _In_     HANDLE hSnapshot,
  _Inout_  LPMODULEENTRY32 lpme
);


i.e. to use an LPMODULEENTRY32

and

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
typedef struct tagMODULEENTRY32
{
    DWORD   dwSize;
    DWORD   th32ModuleID;       // This module
    DWORD   th32ProcessID;      // owning process
    DWORD   GlblcntUsage;       // Global usage count on the module
    DWORD   ProccntUsage;       // Module usage count in th32ProcessID's context
    BYTE  * modBaseAddr;        // Base address of module in th32ProcessID's context
    DWORD   modBaseSize;        // Size in bytes of module starting at modBaseAddr
    HMODULE hModule;            // The hModule of this module in th32ProcessID's context
    char    szModule[MAX_MODULE_NAME32 + 1];
    char    szExePath[MAX_PATH];
} MODULEENTRY32;
typedef MODULEENTRY32 *  PMODULEENTRY32;
typedef MODULEENTRY32 *  LPMODULEENTRY32;


Are you trying to declare your own version of the pre-existing struct?

Andy
Last edited on
@Andy

I have no idea what im doing here to be honest, i understand what a struct is and i understand the data that is inside of it but i have no idea why i need...

why is "tag" here in the struct name?
typedef struct tagMODULEENTRY32

"typedef struct" something left over from C i read?
typedef struct

This looks like its just 2 separate objects of the same struct?
1
2
typedef MODULEENTRY32 *  PMODULEENTRY32;
typedef MODULEENTRY32 *  LPMODULEENTRY32;


Im just learning this all now since i have to use this function.
Last edited on
The Microsoft definition is just to make the use of the structre more C friendly.

as you know, in C you can't use just the name of a struct

1
2
3
4
5
6
7
8
9
struct Example
{
    int one;
    int two;
    int three;
};

Example ex; // ERROR
struct Example ex; // OK 


to get round this, a typedef is provided. You're supposed to use the typedef name rather than the struct name, to avoid having to type struct all over the place.

The name of the struct has "tag" prefixed to it so there is no name collision, but that it's obiously the struct definition used for the typedef.

PMODULEENTRY32 and LPMODULEENTRY32 are identical. The L version is a hangover from the olden (Win16) days when there were near and far pointers.

http://en.wikipedia.org/wiki/Near_pointer#Pointer_sizes
http://en.wikipedia.org/wiki/Far_pointer

In Win16 code it would have been

1
2
typedef MODULEENTRY32 NEAR *  PMODULEENTRY32;
typedef MODULEENTRY32 FAR *  LPMODULEENTRY32;


This is no longer needed with Win32, but the names have stayed. Same deal for LPSTR, which used to be CHAR FAR * (or just char far *)

Andy
Last edited on
Topic archived. No new replies allowed.