Re: passing a string to a dll

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance




"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


.



Relevant Pages

  • Re: passing a string to a dll
    ... Joe, I really appreciate you taking the time to demonstrate this. ... sure how I would implement indexing it for random alphanumeric codes. ... I might handle the array. ... I actually have been wondering if I could use a second string ...
    (microsoft.public.vc.mfc)
  • Re: passing a string to a dll
    ... you can put a STRINGTABLE in a DLL. ... downward, if> estimate upward, etc., but there is a LoadString to load the string. ... I might handle the array. ... look at insertion cost, organization cost, and search cost. ...
    (microsoft.public.vc.mfc)
  • Re: C string array problems (again)
    ... Since there's no obvious reason why that would happen, it'd help if you showed us exactly how you're declaring the function that you send these strings to, and the code that formats the string and sends it along. ... For one thing, an array of a UDT containing dynamic byte arrays is not even remotely similar to char *varand could never substitute for a single VB string, so I don't think we're getting a clear picture of what's going on. ... I take it that your DLL is just a wrapper, and that it's passing the VB arguments along to the underlying CAB DLL? ...
    (microsoft.public.vb.general.discussion)
  • Recieiving an arrray from VB
    ... I have a c++ program that passes information to a VB dll both in .net ... Currently I get back a _bstr_t value for a single string. ... How do I define my return value as an array in c++ so that I define my ... I have seen several examples of sending information to c++ from VB but ...
    (microsoft.public.vc.language)
  • Re: passing a string to a dll
    ... I might handle the array. ... I actually have been wondering if I could use a second string ... look at insertion cost, organization cost, and search cost. ... Putting them in a DLL has effectly zero impact. ...
    (microsoft.public.vc.mfc)