map and LPCWSTR

  • Thread starter Vincent RICHOMME
  • Start date
V

Vincent RICHOMME

Hi,

I would like to use a hashtable to associate a windows unicode string
(define as a ulong *) to a list.
So first I try this :

typedef std::list<FunctionInfo> FuncList;
FuncList g_FuncList;

typedef std::map<LPWSTR, FuncList> ModuleList;
ModuleList g_ModuleList;


void Hook_API( LPWSTR p_wszModuleName,
LPWSTR p_wszFunctionName,
PROC p_pfnNewProc
)
{
ModuleList::iterator l_it;

l_it = g_ModuleList.find( (LPWSTR)p_wszModuleName );
if (l_it == g_ModuleList.end() ){
...
}
}

when I try this if I call three times my function with the same
parameter (L"coredll.dll" for instance) I will have
three keys in my hashtable

Hook_API(L"coredll.dll", "func1", NULL);
Hook_API(L"coredll.dll", "func2", NULL);
Hook_API(L"coredll.dll", "func3", NULL);

So I should have only one key in my hash table (coredll.dll) but it's
not the case


I think it's because map considers LPWSTR as a pointer and not as a
string. So LPWSTR is always different since address is different.

Maybe I should use std::string but in this case how to do with unicode
string.
 
K

Kai-Uwe Bux

Vincent said:
Hi,

I would like to use a hashtable to associate a windows unicode string
(define as a ulong *) to a list.
So first I try this :

typedef std::list<FunctionInfo> FuncList;
FuncList g_FuncList;

typedef std::map<LPWSTR, FuncList> ModuleList;
ModuleList g_ModuleList;


void Hook_API( LPWSTR p_wszModuleName,
LPWSTR p_wszFunctionName,
PROC p_pfnNewProc
)
{
ModuleList::iterator l_it;

l_it = g_ModuleList.find( (LPWSTR)p_wszModuleName );
if (l_it == g_ModuleList.end() ){
...
}
}

when I try this if I call three times my function with the same
parameter (L"coredll.dll" for instance) I will have
three keys in my hashtable

Hook_API(L"coredll.dll", "func1", NULL);
Hook_API(L"coredll.dll", "func2", NULL);
Hook_API(L"coredll.dll", "func3", NULL);

So I should have only one key in my hash table (coredll.dll) but it's
not the case


I think it's because map considers LPWSTR as a pointer and not as a
string. So LPWSTR is always different since address is different.

Maybe I should use std::string but in this case how to do with unicode
string.

I have no idea about unicode. If you feel that using std::wstring or
std::string poses unsurmountable difficulties, there is an alternative: you
can provide a comparison predicate to std::map<>.

#include <map>
#include <cstring>
#include <iostream>

typedef bool (*CStringComparison) ( char const *, char const * );

bool str_less ( char const * lhs, char const * rhs ) {
return ( std::strcmp( lhs, rhs ) < 0 );
}

typedef std::map< char const *, int, CStringComparison > id_table;



int main ( void ) {
char test_array [9] = { 'e', 'r', 0, 'e', 'r', 0, 'a', 'r', 0 };
id_table t ( &str_less );
t[ test_array ] = 0;
t[ test_array + 3 ] = 0;
t[ test_array + 6 ] = 0;
std::cout << t.size() << '\n';
}

This should print 2.

Note: the code is taking a little detour since some compilers fold identical
string constants.


Best

Kai-Uwe Bux
 
O

Ondra Holub

Vincent RICHOMME napsal:
Hi,

I would like to use a hashtable to associate a windows unicode string
(define as a ulong *) to a list.
So first I try this :

typedef std::list<FunctionInfo> FuncList;
FuncList g_FuncList;

typedef std::map<LPWSTR, FuncList> ModuleList;
ModuleList g_ModuleList;
when I try this if I call three times my function with the same
parameter (L"coredll.dll" for instance) I will have
three keys in my hashtable

Hook_API(L"coredll.dll", "func1", NULL);
Hook_API(L"coredll.dll", "func2", NULL);
Hook_API(L"coredll.dll", "func3", NULL);

Hi. You got answer already in
http://groups.google.com/group/comp...c4f11297da0/209998e17b6e22dd#209998e17b6e22dd.
Wasn't it enough?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

STL map and unicode 3
Sorting an STL map 1
Assignment on map 6
sorting map value based 6
map and vector 16
map iterator 2
Appending strings 1
Memory allocation failure in map container 10

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top