Returning string to vb.net

B

Bob

Hi,
I have written a VC++ dll which talks to a Borland C++ dll which needs to
return a string to the VB calling exe.
I thought that if I passed in a stringbuilder by Ref from the VB code the
VC++ dll which is expecting a char* would point it at the returned char* and
all would be well.
I am sure I have used this before but this time no go.
All I get back is the empty string.
Any ideas on this?
The two test message boxes in the code are coming up with the right values
proving that the call into the
Borland dll is OK and a literal assignation of "testing" is also OK
Thanks
Bob
Code follows:
void DNPCONFIG_API LastConfigError(char *sOutput)

{

//char test[255];

//strcpy(test, ca_LastConfigError());

//MessageBox(NULL,test,"",0);


sOutput= ca_LastConfigError();

MessageBox(NULL,sOutput,"",0);

sOutput="testing";

MessageBox(NULL,sOutput,"",0);

//strcpy(sOutput,test);

return;

}
 
J

Jochen Kalmbach [MVP]

Hi Bob!
I thought that if I passed in a stringbuilder by Ref from the VB code the
VC++ dll which is expecting a char* would point it at the returned char* and
all would be well.
void DNPCONFIG_API LastConfigError(char *sOutput)
{
sOutput="testing";
//strcpy(sOutput,test);
return;
}

You should be aware, that the pointer is "passed-into" your function!
With this code you change the pointer!

So please fill the string!
=> strcpy(sOutput, "Test");
and do *not* change the pointer of "sOutput"!

By the way:
It is a bad design to just pass an pointer to an string without passing
the maximum length of the buffer!

Please redesign your function prototype to:

void DNPCONFIG_API LastConfigError(char *sOutput, size_t nMaxLen)


--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/
 
B

Bob

Hi Jochen,
There is something more sinister going on.
strcpy(test,ca_LastConfigError());
strcpy(sOutput,test);


return;


single stepping through the above executes you can see that sOutput in the
locals windows has "Oracle error blah blah"
you can then single step to the return OK
when you single step again up into the VB Calling method you get an error
'An unhandled exception of type 'System.ExecutionEngineException' occurred
in Monitor.exe'
Seeing that the VB code and the VC++ code are both inside try catch blocks I
guess the error is somewhere in between the two.
if you can spot the problem I would be grateful.
Thanks
Bob
*********VB.net Calling code START************
Public Function LastConfigErrorMessage() As String

Try

Dim sb As New StringBuilder

sb.Capacity = 256 ' Desperation

sb.Length = 256 ' likewise

LastConfigError(sb)


Return sb.ToString

Catch ex As Exception

WriteErrorLog("DNP3Config.LastConfigErrorMessage " & ex.Message)

End Try


End Function

*************VB Code END************

*****VC++ code START***************
void DNPCONFIG_API LastConfigError(char *sOutput)

{

try

{

char test[255];

//strcpy(test, ca_LastConfigError());

//MessageBox(NULL,test,"",0);

strcpy(test,ca_LastConfigError());

//sOutput= ca_LastConfigError();

//MessageBox(NULL,test,"",0);

strcpy(sOutput,test);

//MessageBox(NULL,sOutput,"",0);


return;

}

catch (char * str)

{

char error[255];

strcpy(error,str);

MessageBox(NULL,error,"",0);

}

}

************VC++ CODE END******************
 
B

Bob

Hi Jochen,
Solved it.
The library declaration of the string builder was byRef it should have been
by Val.
Thanks
Bob
Bob said:
Hi Jochen,
There is something more sinister going on.
strcpy(test,ca_LastConfigError());
strcpy(sOutput,test);


return;


single stepping through the above executes you can see that sOutput in the
locals windows has "Oracle error blah blah"
you can then single step to the return OK
when you single step again up into the VB Calling method you get an error
'An unhandled exception of type 'System.ExecutionEngineException' occurred
in Monitor.exe'
Seeing that the VB code and the VC++ code are both inside try catch blocks I
guess the error is somewhere in between the two.
if you can spot the problem I would be grateful.
Thanks
Bob
*********VB.net Calling code START************
Public Function LastConfigErrorMessage() As String

Try

Dim sb As New StringBuilder

sb.Capacity = 256 ' Desperation

sb.Length = 256 ' likewise

LastConfigError(sb)


Return sb.ToString

Catch ex As Exception

WriteErrorLog("DNP3Config.LastConfigErrorMessage " & ex.Message)

End Try


End Function

*************VB Code END************

*****VC++ code START***************
void DNPCONFIG_API LastConfigError(char *sOutput)

{

try

{

char test[255];

//strcpy(test, ca_LastConfigError());

//MessageBox(NULL,test,"",0);

strcpy(test,ca_LastConfigError());

//sOutput= ca_LastConfigError();

//MessageBox(NULL,test,"",0);

strcpy(sOutput,test);

//MessageBox(NULL,sOutput,"",0);


return;

}

catch (char * str)

{

char error[255];

strcpy(error,str);

MessageBox(NULL,error,"",0);

}

}

************VC++ CODE END******************
char*
and


You should be aware, that the pointer is "passed-into" your function!
With this code you change the pointer!

So please fill the string!
=> strcpy(sOutput, "Test");
and do *not* change the pointer of "sOutput"!

By the way:
It is a bad design to just pass an pointer to an string without passing
the maximum length of the buffer!

Please redesign your function prototype to:

void DNPCONFIG_API LastConfigError(char *sOutput, size_t nMaxLen)


--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/
 

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