Solved

Problem converting API call from VB6 to .NET

Posted on 2006-06-12
9
439 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
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
 

Author Comment

by:chiche
ID: 16889055
yes it's a COM component
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 85

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 250 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 250 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

867 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

18 Experts available now in Live!

Get 1:1 Help Now