By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,493 Members | 1,304 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,493 IT Pros & Developers. It's quick & easy.

map and LPCWSTR

P: n/a
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<FunctionInfoFuncList;
FuncList g_FuncList;

typedef std::map<LPWSTR, FuncListModuleList;
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.
Feb 2 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Vincent RICHOMME wrote:
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<FunctionInfoFuncList;
FuncList g_FuncList;

typedef std::map<LPWSTR, FuncListModuleList;
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
Feb 2 '07 #2

P: n/a

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<FunctionInfoFuncList;
FuncList g_FuncList;

typedef std::map<LPWSTR, FuncListModuleList;
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....9998e17b6e22dd.
Wasn't it enough?

Feb 2 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.