Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need static strings or equivalent in VB.Net or other help

Status
Not open for further replies.

klamerus

Programmer
Jun 23, 2003
71
US
I'm having a real problem and need some help.

We use a product called Documentum. It's a document manager. There's an API available for both VB and C/C++. This worked quite find until VB.Net came along. The reason follows:

The API works using requests and results in strings parameters. All the procedures return integers that indicate success/failure. For instance an API may request data with the string "get,c,user_name" as the first parameter and the result is written into a second.

Prior to VB.Net the second string needed to be defined something like:

Dim Result as String * 1000

This was necessary because the API "wrote" the result into the memory location provided. That's how it works. This type of declaration is not possible in VB.Net.

The only way that I've found around this is to do something like:

Dim Result as String = " "

With lots of spaces. VB.Net doesn't support the String * XXXXX syntax.

The problem is that the result I get is still one huge string with tons of spaces and the real result in the first few characters. For whatever reason, the Trim commands and such don't work on it.

I'm completely stuck. I need to do something like what I could prior to VB.Net - or at least I need to have a reasonable way to "suck" the good data out of this monster space-padded string, but I can't seem to find any way of doing this.

Help!!!
 
try:
Code:
Dim Result As String = Microsoft.VisualBasic.Space(1000)
 
I don't think that last one will work. I'll just end up with a string like I already have.

I'll give them both a shot though.
 
For whatever reason, the Trim commands and such don't work on it.

That is you're real problem. Both cjelec and TipGiver's solutions should work fine for your declaration. The problem is how you are using .Trim. The String.Trim method Returns a string. It does NOT modify the source.

for example:
Code:
dim s a string = "1.     "
s.trimend
debug.write "'" & s & "'"
'output will be:  '1.     '

what you want is:
Code:
dim s a string = "1.     "
[b]s = [/b]s.trimend
debug.write "'" & s & "'"
'output will be:  '1.'

-Rick

VB.Net Forum forum796 forum855 ASP.NET Forum
[monkey]I believe in killer coding ninja monkeys.[monkey]
 
my declarations are fine, but neither of these work.

The issue is that the API I'm using needs to write into a buffer and it's going to write a 0 terminated string.

This worked with VB and the String * 1000 declaration, but in VB.Net, I can't allocate space, without actually having content.

The API is writing into that and returning fine, but VB.net is oblivious to all of this because it doesn't support this kind of game. It is considering it's management of the buffer to be the correct one and isn't doing anything properly.

I've tried the trim, trimend and other string functions and they're not working. They dont' fail, but they don't get rid of the excess content and if I copy the string into another, it brings along all of the bytes.

In other words, VB seems to consider it's info on the string size as the master (in the spirit of managed code).
 
Strange thought: would a Char array (Dim Buffer() As Char) work???

Or, a slight mod on TipGiver's post:

Dim s As String = String.Empty.PadLeft(1000, Char(0))
 
I've tried the trim, trimend and other string functions and they're not working. They dont' fail, but they don't get rid of the excess content and if I copy the string into another, it brings along all of the bytes.


Okay, simple question: After the API is finished, does you're string contain the 0 terminated data AND the sapces? ie: "abcedfg0 "

If so, post your code, the exact code, that you are using to trim the end. If you variable has that value, then the issue is NOT the declaration, it is the use of the .Trim function.

-Rick

VB.Net Forum forum796 forum855 ASP.NET Forum
[monkey]I believe in killer coding ninja monkeys.[monkey]
 
Without going through some exercise to actually echo each character value to the screen that would be tough. I do happen to know that this is what the routine called does however.

Having said that, I did get this working with some help from a person in experts-exchange. The answer was a little unusual (or unexpected), but it seems to cope with this, so I'll post the code I now have and point out what the fix was so that if someone else finds themselves in a similar situation it will be available.

The following are the declarations:

Declare Function dmVBInit Lib "dmclvb32.dll" () As Integer
Declare Function dmVBDeInit Lib "dmclvb32.dll" () As Integer
Declare Function dmVBExec Lib "dmclvb32.dll" (ByVal asString As String) As Integer
Declare Function dmVBSet Lib "dmclvb32.dll" (ByVal asString As String, ByVal asString As String) As Integer
'Declare Function dmVBGet Lib "dmclvb32.dll" (ByVal asString As String, ByVal asRetVal As String) As Integer
Declare Function dmVBGet Lib "dmclvb32.dll" (ByVal asString As String, ByVal asRetVal As System.Text.StringBuilder) As Integer

The one that I've commented out was the original declaration and I've created a replacement after it.

The next is my routine that now works. Previously, I was not working with stringbuilders, but with strings (which worked prior to VB.Net.

Public Sub ConnectToDocBase()


Dim sCommand As String = ""
Dim sResult As New System.Text.StringBuilder
Dim iResult As Integer = 0

sResult.Length = 1000

Form1.lblStatus.Text = "Status: Connecting to " & Form1.tbDocBase.Text
Application.DoEvents()

sCommand = "connect," & _
Form1.tbDocBase.Text & "," & _
Form1.tbUsername.Text & "," & _
Form1.tbPassword.Text & "," & _
Form1.tbDomain.Text


iResult = dmVBGet(sCommand, sResult)

If iResult = DMFailure Then
Form1.lblStatus.Text = "Status: Failed to connect to " & Form1.tbDocBase.Text
Else
Form1.lblStatus.Text = "Status: Connected to " & Form1.tbDocBase.Text
End If

Application.DoEvents()

End Sub

The stringbuilder class apparently works when unmanaged code writes a zero terminated character string into it. The first attempt failed, but once I gave it a length (and therefore a buffer) it works fine. The data is written, there are no access violations, and the class "understands" to get the string properly.

The previous examples where I used a string variable in vb.net and set the size "big" by setting it equal too a big spaced string worked, but the trim and endtrim methods of teh string class did absolutely nothing. Also, when I set some new string equal to the original (which had been written into), it got the whole spaced padded buffer.

Sort of interesting that I got "backed into" using this new stringbuilder class that I previously hadn't been especially keen on working with.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top