Solved

Problem converting API call from VB6 to .NET

Posted on 2006-06-12
9
446 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
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 

Author Comment

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

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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

726 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