Solved

Problem converting API call from VB6 to .NET

Posted on 2006-06-12
9
436 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
  • 5
  • 3
9 Comments
 
LVL 9

Expert Comment

by:sk33v3
Comment Utility
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
Comment Utility
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
Comment Utility
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
 

Author Comment

by:chiche
Comment Utility
yes it's a COM component
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 85

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 250 total points
Comment Utility
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 250 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

744 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now