Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / June 2005

Tip: Looking for answers? Try searching our database.

Returning string to vb.net

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Bob - 23 Jun 2005 06:12 GMT
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;

}
Jochen Kalmbach [MVP] - 23 Jun 2005 07:32 GMT
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)

Signature

Greetings
  Jochen

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

Bob - 24 Jun 2005 02:38 GMT
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******************
> Hi Bob!
>
[quoted text clipped - 23 lines]
>
> void DNPCONFIG_API LastConfigError(char *sOutput, size_t nMaxLen)
Bob - 24 Jun 2005 03:44 GMT
Hi Jochen,
Solved it.
The library declaration of the string builder was byRef it should have been
by Val.
Thanks
> Hi Jochen,
> There is something more sinister going on.
[quoted text clipped - 112 lines]
> >
> > void DNPCONFIG_API LastConfigError(char *sOutput, size_t nMaxLen)

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.