[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Problem converting API call from VB6 to .NET

Posted on 2006-06-12
9
Medium Priority
?
455 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying 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

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.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…

650 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