Re: passing a string to a dll
- From: "Giovanni Dicanio" <giovanni.dicanio@xxxxxxxxxx>
- Date: Wed, 19 Sep 2007 15:16:50 +0200
"Joseph M. Newcomer" <newcomer@xxxxxxxxxxxx> ha scritto nel messaggio
news:3qc1f39uj6cqkg3j0tr041d67saln7l5h0@xxxxxxxxxx
The real purpose of this particular DLL is to store thousands of****
alphanumeric codes. Presently, I do this in a function in the exe,
building
a large CStringArray to which each constant is added, e.g.,
array.Add(_T("K34TU79456T1") ); When a string (code) is passed to this
function, an iteration searches for that string in the array and returns a
bool value accordingly. The call occurs only once during the running of
the
exe. This has been working very effectively, but perhaps not so
efficiently; I am not sure how to evaluate the performance other than that
the results are instantaneous and accurate.
This is probably a bad design. Use CMap or std::map, at the very least.
An array is just
about the worst possible choice when thousands of keys are involved, since
the time to
search is O(n/2), whereas for hash tables it can be O(k) for some small
integer k;
std::map is fairly fast, although I'm not sure what its complexity is (I
haven't checked),
but it going to be faster than O(n/2).
My understanding of the OP's problem is the following:
o He stores strings into an array; these strings represent valid codes.
o This array is initialized only once in program life-cycle, at startup.
o He has a function like so:
bool IsValidCode( string codeToCheck )
that searches the input string in the valid-codes array; if the string
is found, then it represents a valid code, and the function returns true;
else the function returns false.
If my understand is correct, I don't very much agree with what Joe suggests
(maybe I have not understood Joe well).
In fact, I think that using the array is just fine in that context.
The array is initialized only once to store the strings representing valid
codes.
The trick here IMHO is to be sure that the array is *sorted*.
In fact, if the array is sorted, then a simple *binary search* with a
O(log(n)) asymptotic complexity would be just fine.
And I think it would be even better than a map, which I think has worse
performance than binary-search's O(log(n)).
Moreover - IIRC, but take it with a grain of salt - I think that std::map is
actually *not* a hash-map, but a BST (binary search *tree*).
I think that the real hash-map is std::hash_map.
But, again, I think that sorted array + binary search would be just fine...
You would need to say more about this list of codes. Are they some
attempt to do license
keys? If so, it would take most 14-year-olds under ten minutes to crack
it.
I completely agree with Joe on this point.
If I move these strings into the DLL function, I will be happy to get them****
out of the exe; but what impact does this have on performance, memory
consumption or anything else? Am I gaining or losing anything?
Zero. Putting them in a DLL has effectly zero impact. Well, there's a
small cost to load
Maybe the effect would be to make it easier for the cracker to break the
protection: the DLL substitution with a cracked DLL that just returns OK
when codes are checked would be just fine :(
Otherwise, you would use LPCTSTR in the usual (old-fashioned, error-prone,
possibly-storage-leaking) way it has always been used.
There are contexts where LPCTSTR (or better const wchar_t * or even BSTR
IMHO, as COM does) are OK.
For example: if the DLL has a C *interface* (it's OK to use a string class
*inside* the DLL, of course).
BTW: BSTR is even better than wchar_t *, because with wchar_t* it requires
O(n) to find the string length, while with BSTR is is just optimal O(1),
being the BSTR length-prefixed.
Giovanni
.
- Follow-Ups:
- Re: passing a string to a dll
- From: SteveR
- Re: passing a string to a dll
- From: Joseph M . Newcomer
- Re: passing a string to a dll
- References:
- passing a string to a dll
- From: SteveR
- Re: passing a string to a dll
- From: Joseph M . Newcomer
- Re: passing a string to a dll
- From: SteveR
- Re: passing a string to a dll
- From: Tom Serface
- Re: passing a string to a dll
- From: SteveR
- Re: passing a string to a dll
- From: Joseph M . Newcomer
- passing a string to a dll
- Prev by Date: Re: converting ANSI to UNICODE
- Next by Date: Re: Flicker free scrolling in a CFormView - ClistCtrl
- Previous by thread: Re: passing a string to a dll
- Next by thread: Re: passing a string to a dll
- Index(es):
Relevant Pages
|