Posted: 17th May 2007 19:27
I created this thread to discuss this topic, as the original by Jasuk70 is too old to post in.

For reference, this is Jasuk70's original post:

http://forum.thegamecreators.com/?m=forum_view&t=68687&b=1

And this is IanM's utility post, which is also a necessary part of this discussion:

http://forum.thegamecreators.com/?m=forum_view&b=18&t=85209&p=0


The Issue
==========

I have this code working to an extent. I have used Jason's framework, and changed the application to my own.
Next, I changed the enhancements File Map commands and replaced them with IanM's Memory Bank equivalents, as suggested by Wolf. This does indeed bring the whole thing to life, and the heartbeat works. The mouse coordinates are passed and messages can be exchanged.

The problem is that I now have a black DB Pro screen, rather than my application. As you can see in the image below, I had a visible screen, until I replaced the Memory Bank commands...



I have tracked this down to the following line of code in Jason's ProcessMessage() function:

aMessage=bank String$(cMAPSEND,0, 1024)

This retrieves the message from the memory bank in order to respond. I think it is probably the bank size that causes the problems.
In the VB app, it is 2048 bytes. I have tried this value, and also 1024 to prevent it running over. The data itself is about 10 bytes long, but could be any length. With the original File Map commands, no size was required so I have nothing to relate it to.
Other similar commands work, such as the heartbeat. But here, I know the length is exactly 4 bytes. Hence my reasoning that the length is the issue.

Any profound thoughts on the matter?
Posted: 17th May 2007 19:40
1p
Posted: 17th May 2007 20:33
Batvink,
One thing to be careful of (and might be what is causing your problem)....
If you run the VB app and it starts you DBpro app, your DBpro App may get started in the Visual Basic editor INSTEAD of your VB application. To prevent this from happening, make sure you actually close the form in your Visual Basic editor. This problem still gets me all of the time. You can hear "Doh!" a lot when I work on my GUI because of this issue.

Another reccomendation. In your DBpro app's display settings, make sure you select "Hidden" as the display mode. This will prevent DBpro from fighting with VB to be active. It still displays, it just doesn't try to become the active window.
Also, another thing to note. You have to pass all of the user input through VB to your DBpro app. So, that means any key presses and/or mouseclicks have to come through VB to DB. This complicates things on DBpro's side a bit, but it is worth the effort.
Posted: 17th May 2007 21:07
I'd also suggest that you don't use strings to pass events unless you actually need to pass a string. If you were sending event across the network, then it may be warranted to avoid byte-order issues, but not when passing data through memory.

The size parameters for the bank string commands were a deliberate design decision on my part - I did it that way to avoid accidental overwriting of data further into the bank, and to avoid attempting to read a string where the terminating null byte had been overwritten.

If you specify 10 characters and attempt to write 11, the overflow characters are silently dropped. If you specify 10 characters and only attempt to write 5, then the final 5 characters in the bank are filled with nulls. In that case, when you attempt to read 10 characters, you will only get the 5 you actually wrote.

Here's a small test program you can play around with:
+ Code Snippet
sync off

make bank 1, 30

for i = 0 to get bank size(1)-1
   write bank byte 1, i, rnd(255)
next
make bank from bank 2, 1

write bank string 1, 0, "Hello World", min(get bank size(1), 20)

for i = 0 to get bank size(1)-1
   text 0, i*15, str$(i)
   text 50, i*15, str$(bank byte(2, i))
   text 100, i*15, str$(bank byte(1, i))
next

x$ = bank string$(1, 0, min(get bank size(1), 20))

set cursor 0, get bank size(1) * 15
print "String value read back is "; x$
print "String length is "; len(x$)

wait key


The bank is filled with random bytes, then a string is written to it, and finally, the bytes are displayed (columns are byte number, value before, value after).

Try changing the size of the bank to a value under 11 to see how it's handled. Don't increase above 30 unless you increase your resolution.
Posted: 17th May 2007 21:31
make sure you actually close the form in your Visual Basic editor
select "Hidden" as the display mode
You have to pass all of the user input through VB to your DBpro app. So, that means any key presses and/or mouseclicks have to come through VB to DB

I spent an hour reading and implementing Jason's stuff, so I have most of it covered. I've now also made it hidden, which I hadn't done.

Ian - the strings are actually being sent successfully. I am outputting the data to the screen, and it's all in tact. But when I add that 1 line to read the string, the screen goes black

[EDIT] DOH!!!! I can't believe what the problem was, given that Wolf has already told me that this is the biggest issue with DBP in a VB app.

The message is actually a "Resize WIndow" trigger. So the window gets resized and DBP loses everything, just as it would with a normal DBP app.

Time to add my object reload function to the mix, methinks.

Thanks guys.
Posted: 17th May 2007 21:59
Are you changing the resolution when you get a resize? You might consider just changing the window size instead with SET WINDOW SIZE - that doesn't cause any media to be unloaded because it doesn't change the resolution.
Posted: 17th May 2007 22:13
Are you changing the resolution when you get a resize? You might consider just changing the window size instead with SET WINDOW SIZE - that doesn't cause any media to be unloaded because it doesn't change the resolution.


I'm pretty sure that if you do that the DBpro window becomes disconnected from the VB control.
Posted: 18th May 2007 1:31
SET WINDOW SIZE gives the window a border and a title bar unfortunately.

This is coming together quite nicely. I can pick up the mouse movement and clicks, and have converted some of my camera movements to prove I can do what I used to do when it was standalone.

I have a simple question though How do I set the mouse position in VB? I want the mouse to stay where it is when I click and drag in the DBPro window.
Posted: 23rd May 2007 19:37
I have tried to stop the mouse moving under certain circumstances (clicking and draging the scene). But when I do, the mouse "jumps" to a different position initially. I'm doing it as part of the VB code, anyone see my problem?

This is the Mouse module, which incorporates the calls to movingthe mouse programmatically:
+ Code Snippet
Module mouse
    'Option Explicit

    Public Declare Sub Mouse_Event Lib "user32" (ByVal dwFlags As Long, ByVal dx As Long, ByVal dy As Long, ByVal cButtons As Long, ByVal dwExtraInfo As Long)

    Public Declare Function SetCursorPos Lib "user32" (ByVal X As Long, ByVal Y As Long) As Long

    Public Declare Function GetCursorPos Lib "user32" (ByVal lpPoint As POINTAPI) As Long
    Public Const MOUSEEVENTF_LEFTDOWN As Integer = &H2
    Public Const MOUSEEVENTF_LEFTUP As Integer = &H4
    Public Const MOUSEEVENTF_MIDDLEDOWN As Integer = &H20
    Public Const MOUSEEVENTF_MIDDLEUP As Integer = &H40
    Public Const MOUSEEVENTF_RIGHTDOWN As Integer = &H8
    Public Const MOUSEEVENTF_RIGHTUP As Integer = &H10
    Public Const MOUSEEVENTF_MOVE As Integer = &H1

    Public Structure POINTAPI
        Dim X As Long
        Dim Y As Long
    End Structure

    Public Function GetCurrentX() As Long
        Dim Position As POINTAPI
        GetCursorPos(Position)
        GetCurrentX = Position.X
    End Function

    Public Function GetCurrentY() As Long
        Dim Position As POINTAPI
        GetCursorPos(Position)
        GetCurrentY = Position.Y
    End Function

    Public Sub LeftClick()
        LeftDown()
        LeftUp()
    End Sub

    Public Sub LeftDown()
        Mouse_Event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0)
    End Sub

    Public Sub LeftUp()
        Mouse_Event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)
    End Sub

    Public Sub MiddleClick()
        MiddleDown()
        MiddleUp()
    End Sub

    Public Sub MiddleDown()
        Mouse_Event(MOUSEEVENTF_MIDDLEDOWN, 0, 0, 0, 0)
    End Sub

    Public Sub MiddleUp()
        Mouse_Event(MOUSEEVENTF_MIDDLEUP, 0, 0, 0, 0)
    End Sub

    Public Sub MoveMouse(ByVal xMove As Long, ByVal yMove As Long)
        Mouse_Event(MOUSEEVENTF_MOVE, xMove, yMove, 0, 0)
    End Sub

    Public Sub RightClick()
        RightDown()
        RightUp()
    End Sub

    Public Sub RightDown()
        Mouse_Event(MOUSEEVENTF_RIGHTDOWN, 0, 0, 0, 0)
    End Sub

    Public Sub RightUp()
        Mouse_Event(MOUSEEVENTF_RIGHTUP, 0, 0, 0, 0)
    End Sub


End Module


In the MouseDown event of the DBPro container, I save the coordinates:
+ Code Snippet
            MouseLastX = e.X
            MouseLastY = e.Y


and in the MouseMove event, I keep moving the mouse back from whence it came:
+ Code Snippet
SetCursorPos(MouseLastX, MouseLastY)
Posted: 23rd May 2007 23:18
OK, I found the cursor class. I still have issues, but that is with the comms side of the setup. Brainache
Posted: 5th Jun 2007 16:10
I've asked this question in IanM's thread, but it may be just as relevant here. It's about getting / setting integers and floats in Shared memory in VB.

Quick question about file banks...you can WRITE BANK FLOAT and WRITE BANK INTEGER, and obviously get BANK FLOAT and get BANK INTEGER.
Does anyone know what the VB.net equivalents are? There only seems to be SharedMemory.getByte, SharedMemory.SetByte, and SharedMemory.ToString - no Singles or Integers
Posted: 6th Jun 2007 15:10
I don't know if this will help, but I came across this today while browsing through the CodeProject site : http://www.codeproject.com/dotnet/ShareImageAcrossProcesses.asp

It's C# but I don't think you'll have too much trouble porting/interpreting it for VB.NET
Posted: 6th Jun 2007 17:01
Thanks IanM. JohnY showed me some valuable stuff with the BitConverter class yesterday, which I have working today. I plan to post an improved version of Jasuk70's code once I have it perfected. There's nothing wrong with the original, in fact there is so much untapped potential in it that I am just starting to discover.

The link you posted gives me more ideas for this method. The potential for sharing web cams and other resources is interesting.
Posted: 20th Jun 2007 4:57
Would anyone mind translating the VB.NET code to C#?