473,322 Members | 1,421 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Returning string to vb.net

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;

}

Nov 17 '05 #1
3 1462
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/
Nov 17 '05 #2
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******************
"Jochen Kalmbach [MVP]" <no********************@holzma.de> wrote in message
news:Of**************@TK2MSFTNGP15.phx.gbl...
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/

Nov 17 '05 #3
Bob
Hi Jochen,
Solved it.
The library declaration of the string builder was byRef it should have been
by Val.
Thanks
Bob
"Bob" <bo*@nowhere.com> wrote in message
news:uz**************@tk2msftngp13.phx.gbl...
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******************
"Jochen Kalmbach [MVP]" <no********************@holzma.de> wrote in message news:Of**************@TK2MSFTNGP15.phx.gbl...
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/


Nov 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

25
by: Victor Bazarov | last post by:
In the project I'm maintaining I've seen two distinct techniques used for returning an object from a function. One is AType function(AType const& arg) { AType retval(arg); // or default...
13
by: Matthias Kaeppler | last post by:
Hi, I was wondering why library implementors often make getter functions return strings by value (copies). For example, in boost::filesystem the leaf() function returns an std::string by value....
5
by: Alfonso Morra | last post by:
Hi, What is the recomended way of returning an STL container (e.g. std::string, std::vector etc fom a function? Is it by simply returning a local variable? (I doubt it) std::string...
7
by: wonderboy | last post by:
Hey guys, I have a simple question. Suppose we have the following functions:- //-----My code starts here char* f1(char* s) { char* temp="Hi"; return temp;
1
by: Randy | last post by:
Hello, I have a web service in which I'm doing a query to an Access database and returning the resulting XML data when I do the return from the service... public string AOS_Data(string sql) {...
5
by: Stacey Levine | last post by:
I have a webservice that I wanted to return an ArrayList..Well the service compiles and runs when I have the output defined as ArrayList, but the WSDL defines the output as an Object so I was...
2
by: Asim Qazi | last post by:
Hi All public class MyResponse { public bool m_bStatus; public string m_szErrorCode; public string m_szMessage; }
13
by: Karl Groves | last post by:
I'm missing something very obvious, but it is getting late and I've stared at it too long. TIA for responses I am writing a basic function (listed at the bottom of this post) that returns...
7
by: Thomas Lenz | last post by:
Please consider the following code snippet: string myfunction() { ostringstream oss; oss << "junk"; // do something more with oss; I can't make it const... return oss.str(); } Is the...
8
by: darren | last post by:
Hi everybody, have a quick look at this code: ===== ===== int main(void) { string msg; makeString(msg); cout << "back in main, result = " << msg << endl;
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.