?
Solved

Problem converting API call from VB6 to .NET

Posted on 2006-06-12
9
Medium Priority
?
450 Views
Last Modified: 2012-06-27

I have written a VB6 application that was calling a separate vendor's API this way:

Dim ErrText as String
Dim f as Boolean

ErrText = String (2560, vbNull)

f = MosServer.Logon (ErrText)

VERY IMPORTANT: the API reference indicates that ErrText is passed BY REFERENCE (ByRef)

When converting to .NET I write:

Dim ErrText as String
Dim f as Boolean

ErrText= ".......... " (note: I put 2560 spaces, I don't know an equivalent for the String() function)
f = MosServer.Logon (ErrText)

I get an error:

cast from string "" to type 'integer' is not valid


SO my question is: how do I replicate the "String (2560, vbNull) " in .NET
but more importantly, how do pass the string by ref without getting the error?

0
Comment
Question by:chiche
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
9 Comments
 
LVL 9

Expert Comment

by:sk33v3
ID: 16888923
strdup(2560,chr(0))

This will send the null characters however the error you are reporting is that someone is trying to cram your string into a 4 byte memory size... if this is the case you will always receive this error. Have you tired just passing 4 bytes of information?
0
 

Author Comment

by:chiche
ID: 16889017
I guess the 4 bytes should be the address of the string

The API function is expecting a BYREF string, so that it can return the ErrText
I guess that it expects the address of the string and not the string itself..  

Maybe there's a function in .NET to pass the address instead of the string.. but isn't what the ByRef definition is already supposed to do??

0
 
LVL 9

Expert Comment

by:sk33v3
ID: 16889040
Yes and no. You must look back long ago into the days before dot net. In those days and even still people sent the address of the location of the memory. This is very common to see. I assume you are probably using a COM component?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:chiche
ID: 16889055
yes it's a COM component
0
 
LVL 86

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 1000 total points
ID: 16889161
Leave the API declaration as is and try passing in a StringBuilder instead...

        Dim ErrText As New System.Text.StringBuilder(2560)
        Dim f As Boolean = MosServer.Logon(ErrText)
        MsgBox(ErrText.ToString)
0
 
LVL 9

Accepted Solution

by:
sk33v3 earned 1000 total points
ID: 16889196
ok then try passing the string as an integer and assign the integer as the address of the variable. Here is a good article in regards to that.

http://www.codeproject.com/vb/net/Marshal.asp
0
 

Author Comment

by:chiche
ID: 16889269
I just realized that the error was coming from the passing of another argument (I didn't show it to simply the question)

Now, I get the error:

An unhandled exception of type 'System.NullReferenceException' occurred in AvayaSfa.exe

Additional information: Object reference not set to an instance of an object

The only difference being that I changed

ErrText = String (2560, vbNull)  IN VB6

to

ErrText = StrDup(2560, Chr(0) in .NET
0
 

Author Comment

by:chiche
ID: 16889422
Ok, in fact I realized that the erorr isn't coming at all from the string but from the way my API is instanciated

I have a form frmMAIN
--------------------------------------
in VB6

Public WithEvents MosServer As Moagent32.IMoagent

Function xx()  as Boolean
a = MosServer.SomeMethod()
End FUnction
----------------------------------------
in .NET

Public Class frmMAIN
    Public WithEvents MosServer As Moagent32.IMoagent
    Function xx()  as Boolean
      a = MosServer.SomeMethod()
    End FUnction
End Class

-------------------------------------------

In VB6 the object MosServer  IS NOT INSTANCIATED.

Does it have to be in .NET?




0
 

Author Comment

by:chiche
ID: 16889545
Ok, I found out what the problem is and it has nothing to do with my original question.
I will however split the points for time spent.
Thanks
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses
Course of the Month12 days, 12 hours left to enroll

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question