Newbie

S

Sueffel

Okay, I'm new to VC++, but have used VB6 for a while, and have moved to
VB.NET, but, have to devulge into C because of a couple of things:
First and foremost, Function Exporting. Then, ofcourse, the fact that no
matter how much obfuscation you do to a .NET assembly, you can get the code
with a little work. Then there's speed ofcourse.

Okay, here's where I'm at. I have my DLL made, learned how to export my
function, learned about vectors, and made a test function as follows:

__declspec(dllexport) int EatMe(int Input[])
{
vector <int> vi( 10, 5);
vi[2] = Input[2];
return vi[2];
}

Not too shabby I'd say. Now, what I need to do is this:
First, I need to pass this a string, sorry, still in VB mode, a char array.
I'm going make it's upper limit 255, but can this be done as a vector? Now
comes the fun part, I need to take that char array, convert each charactor
to it's ASCII value, then return the 3 digit code, so I need it to also be
char data type. Confusing? Yes, lemme break it down further. If I send it
the string "Hello", I need to convert it to it's ASCII equiv as such:

[0] 072
[1] 101
[2] 108
[3] 108
[4] 111

And return it. I cannot find anything on returning charactor arrays, and,
since I don't know how big the incoming stuff is, I need to use a vector,
then return the data. I have scoured for a lil while and found nothing.
Help???

Thanks,
Sueffel
 
S

Sven Carstensen

Hi Sueffel,

Sueffel said:
Okay, I'm new to VC++, but have used VB6 for a while, and have moved to
VB.NET, but, have to devulge into C because of a couple of things:
First and foremost, Function Exporting.

Why is function exporting of any importance to you? With .Net you do "type
exporting" which is by far more powerful.
Then, ofcourse, the fact that no
matter how much obfuscation you do to a .NET assembly, you can get the code
with a little work.

Hmmm - so what?
Then there's speed ofcourse.

Did you do benchmarking between .Net and raw C++? C++ is not by itself
faster.
Okay, here's where I'm at. I have my DLL made, learned how to export my
function, learned about vectors, and made a test function as follows:

__declspec(dllexport) int EatMe(int Input[])
{
vector <int> vi( 10, 5);
vi[2] = Input[2];
return vi[2];
}

I guess this is just a sample with no usefull functionality?
Not too shabby I'd say. Now, what I need to do is this:
First, I need to pass this a string, sorry, still in VB mode, a char array.
I'm going make it's upper limit 255, but can this be done as a vector? Now
comes the fun part, I need to take that char array, convert each charactor
to it's ASCII value, then return the 3 digit code, so I need it to also be
char data type. Confusing? Yes, lemme break it down further. If I send it
the string "Hello", I need to convert it to it's ASCII equiv as such:

[0] 072
[1] 101
[2] 108
[3] 108
[4] 111
And return it.

Hmm, when you have a C-style string in a zero terminated character array
then you already have you ASCII values in that array. Can you get more
explicit on what kind of transformation you need? Could you give a prototype
of the transforming function you have in mind?
I cannot find anything on returning charactor arrays, and,
since I don't know how big the incoming stuff is, I need to use a vector,
then return the data. I have scoured for a lil while and found nothing.

Returning an stl vector has some implications when it comes to exported
functions: All your modules which exchange stl containers must use the same
c-runtime dll so that they use the same heap. Otherwise you will end up with
crashes because one module is allocating the vector elements and another
module is deallocating within another heap.

If you really want to use raw C/C++ modules with exported functions you
should maybe stick with basic data types. In your case that might be:

size_t MyExportFunc(const char* InputString, char* OutputString, size_t
SizeOfOutputString);

This would give the caller the possibility to allocate the output buffer and
specify the maximum size of that buffer in SizeOfOutputString. The return
value would give the caller the used amount in the output buffer.

HTH,
Sven
 
B

Bonj

A 'string' to VB is a 'BSTR' to C++, and to pass a string from VB to C++ is
best done with it declared as a BSTR on the C++ side (but you get away with
a LPTSTR / char*) and to return a BSTR from C++ to VB (i.e. so VB is getting
a string back that it can use) you *need* to call SysAllocStringByteLen.
Once you have a char* in C++, this is a char array so the members of this
array are unsigned shorts, which represent the ASCII values of the
individual letters.
BTW, although strings are handled effortlessly by VB, a BSTR in itself is a
complex beast so as soon as you receive it into C++, convert it to something
else, possibly via the more friendly _bstr_t.
 
S

Sueffel

<SNIP>

To address Sven first:

Well, as far as the obfuscation, the fuctionality that this will need to
perform must be kept under wrapts, and I origionally did it under VB6, but
it was slow, but hard to get the code. I have searched for almost a year on
a way of protecting any code in .NET that I put time into. I believe in
Open Source, but there must be a level of choice for the developer, I may
not release the code for this perticular thing, but I may release the DLL
Free, but not under GNU/GPL. But, nuff on my spew.

Now to answer both:

Here's the function under VB.NET:

Protected Function convertToCode(ByVal Input As String) As String()
Dim OutStr() As String
Dim I As Integer

ReDim OutStr(Input.Length - 1)

For I = 0 To Input.Length - 1
OutStr(I) = Asc(Input.Substring(I, 1))
If OutStr(I).Length < 3 Then OutStr(I) = New String("0", 0, 3 -
OutStr(I).Length) & OutStr(I)
Next

Return OutStr
End Function

Now, I could just make it a byte array, then convert it, but this is just as
fast with a little less code. Anyhew, this is the basis of what I need to
start with. I'm thinking I'm getting in way over my head, so I probably
will have to put this project on the back burner until either A) I learn
C++, or B) Find a way to protect my codebase.

Thanks again,
Sueffel
 
S

Sven Carstensen

Hi Sueffel,

Sueffel said:
Here's the function under VB.NET:

Protected Function convertToCode(ByVal Input As String) As String()
Dim OutStr() As String
Dim I As Integer

ReDim OutStr(Input.Length - 1)

For I = 0 To Input.Length - 1
OutStr(I) = Asc(Input.Substring(I, 1))
If OutStr(I).Length < 3 Then OutStr(I) = New String("0", 0, 3 -
OutStr(I).Length) & OutStr(I)
Next

Return OutStr
End Function

Do you want your C++ function to be called from VB so that your clients will
need BSTRs or are your callers C++ users, too?

If VB: as Bonj wrote you can take the BSTR as LPCTSTR parameter. The return
value needs to be a variant array of BSTRs.
If C++: a char array consist already of the ASCII codes, so you just take
yourString to get the ASCII code. If you need the formatting as a three
digit string you could use something like this:

std::vector<std::string> outValues;
char sz[10];
for(int i = 0; i < lstrlen(yourString); ++i)
{
_snprintf(sz, sizeof(sz)-1, "%03ld", yourString;
outValues.push_pack(sz);
}

Problem here is in passing the vector and string objects around between
modules as you have to make sure they will use the same runtime dll so that
new and delete calls operate on the same heap

Am I somewhere close in understanding your question? Maybe you could get a
bit more specific on the task you want to solve.

Bye,
Sven
 
S

Sueffel

Sven Carstensen said:
Hi Sueffel,

Sueffel said:
Here's the function under VB.NET:

Protected Function convertToCode(ByVal Input As String) As String()
Dim OutStr() As String
Dim I As Integer

ReDim OutStr(Input.Length - 1)

For I = 0 To Input.Length - 1
OutStr(I) = Asc(Input.Substring(I, 1))
If OutStr(I).Length < 3 Then OutStr(I) = New String("0", 0, 3 -
OutStr(I).Length) & OutStr(I)
Next

Return OutStr
End Function

Do you want your C++ function to be called from VB so that your clients will
need BSTRs or are your callers C++ users, too?

If VB: as Bonj wrote you can take the BSTR as LPCTSTR parameter. The return
value needs to be a variant array of BSTRs.
If C++: a char array consist already of the ASCII codes, so you just take
yourString to get the ASCII code. If you need the formatting as a three
digit string you could use something like this:

std::vector<std::string> outValues;
char sz[10];
for(int i = 0; i < lstrlen(yourString); ++i)
{
_snprintf(sz, sizeof(sz)-1, "%03ld", yourString;
outValues.push_pack(sz);
}

Problem here is in passing the vector and string objects around between
modules as you have to make sure they will use the same runtime dll so that
new and delete calls operate on the same heap

Am I somewhere close in understanding your question? Maybe you could get a
bit more specific on the task you want to solve.

Bye,
Sven
Now, I could just make it a byte array, then convert it, but this is
just
as
fast with a little less code. Anyhew, this is the basis of what I need to
start with. I'm thinking I'm getting in way over my head, so I probably
will have to put this project on the back burner until either A) I learn
C++, or B) Find a way to protect my codebase.

Thanks again,
Sueffel


Wow, you just blew me out of the water! LOL. I definatly need to set this
onto the side and take a couple of courses on C.

Thanks again,
Sueffel
 

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

Top