Web development by PHP-Web-Host.com   Website Hosting by PHP-Web-Host.com
Web Hosting by PHP-Web-Host.com

SoftSmart Blogs!

SoftSmart Technical, Software and Web Related Blogs

Tel (Lon, UK): 020 8133 3460

 

 

Blogs

Archive for the ‘Visual Basic (VB)’ Category

Compiling a C++ dll with MinGW to use with Visual Basic

Tuesday, February 16th, 2010

Using a dll compiled with C++ in visual basic is really rather easy, its just a little hard to find good (EASY) information of how to do it. There is a fair amount of information for Microsoft’s C++ compilers, but what about us who write in a text editor and compile with g++ on MinGW? This post will show you how you can be using a c++ dll in visual basic.

I’m a big fan of ANSI C++, MinGW and the g++ compiler. I don’t like MS C++ compilers for various reasons. One of those is that I don’t believe it generates ANSI compliant code, and I certainly can’t compile for Linux from it.

If you are a visual basic (VB) coder, it may be necessary at times to use a dll compiled in C++. Or, you may be in a similar situation to myself: all of your projects are written in VB and you are porting everything over to C++. However, you have a problem. Porting everything at once will take a long time. If you port a module to C++, you would want to start using that module straight away with your existing VB projects (until you have ported the whole lot).

So, here is a quick guide on accomplishing that. This guide is very simple and is intended to get you started in the right direction.

Create a cpp file called “test.cpp”. In this file, paste the following code:

// Start of file

extern “C” // only required if using g++
{

__declspec (dllexport) int __stdcall FooBar (void)
{
return 343;
}

__declspec (dllexport) int __stdcall TimesTwo (int val)
{
return val * 2;
}

}

// end of file

Now we need to compile our dll. There are two steps (maybe they can be converted into a single step, but as I said, this is a beginners guide and I’m not an expert (yet) on the g++ compiler).

To compile, in MinGW, cd to the directory that has your cpp file and type:

g++ -c -Dtest_dll test.cpp

This will create a file called test.o

Now type

g++ -shared -o test.dll test.o -Wl,–add-stdcall-alias

This will create test.dll

Now, open a normal VB  project and in the form’s code space, paste the following

Option Explicit

Private Declare Function FooBar Lib “C:\msys\1.0\home\John\Dll_with_VB_test\test.dll” () As Integer
Private Declare Function TimesTwo Lib “C:\msys\1.0\home\John\Dll_with_VB_test\test.dll” (ByVal val As Integer) As Integer

Private Sub Form_Load()

Debug.Print FooBar

Debug.Print TimesTwo(5)

End Sub

Take note of two things here:

1) The path to the dll must obviously be changed to be correct on your system

2) In the second declare, we pass an integer called val in to the dll. If you leave out the ByVal, the value is corrupted, so the ByVal bit is important.

That’s it, please post back here if you have any useful insights. Lets learn (and port our software) together!

 


 

Starting Windows Services in VB

Saturday, January 16th, 2010

At times it may be necessary for you to control windows services in your visual basic code. For instance, one of the applications I had written used various software modules to write to and read from a MySQL database. I then used IIS to connect to the database and display reports to users on the network in a web browser.

The problem came in when MySQL version 4 must have had a little bug, and it did not work very well with IIS. When there was a relatively large amount of data in the recordset, MySQL would fall over with the annoying little message: “MySQL has gone away”. Later on, when version 5 became available, I installed that and it solved my problems, BUT in the interim I had a problem. I couldn’t tell my clients to wait around for newer versions of MySQL, and I also didn’t want to rewrite components to use another database.

So, the solution was to trap the errors that MySQL crashes generated and automatically restart MySQL as a windows service. This could be any service though, I just happened to use it for MySQL.

If you go to control panel –> administrative tools –> Services, you will see a list of services there with their service names. Use this name with the code in the download below to stop, start or pause your windows services. I am not going to give a detailed description of every line of code (because honestly I don’t really know most of it, its alot of API calls!). Instead, by supplying the source code, I hope that you will be able to modify this and use it in your own code.

Restarting Windows Services with Visual Basic Start windows services in VB

 


 

Visual Basic Chess Game

Thursday, January 14th, 2010

This source code for a chess game written in visual basic is another tutorial that is dumped into the intermediate category, however, I think it could really fit into the beginner and advanced categories too. The reason for this is that there aren’t really any advanced things going on here in terms of strict visual basic code, but the logic may be a little tough, especially as this was one of my early projects, so in some cases the naming conventions suck, ie, are really non existent, and some things are just done the long way round. However, having said that, it still works and that’s pretty cool.

This is not a tutorial in the sense that I am going to ramble on here about writing a chess game in visual basic. This is rather another tutorial where I say, here is the source code, step though it and see what you like, see what you don’t, see where you can improve, but learn from each of those steps. There really is just too much in the project to talk about here.

I will just mention a few things:

1) One of the things to learn from this code is drag and drop. This visual basic chess game uses drag and drop to drag chess pieces around the board and drop them into the square they are moving to. This is a little trickier than it may sound at first because often times the piece is not dragged and dropped cleanly into a single square. It may be 90% in a square, but not exactly. You will see that there is a little calculation that goes on to determine which square it was dropped into “the most”.

2) Many routines simplify logic. I think that I probably could have even created more routines to simplify even further. Now I am not talking about arbitarily adding routines, I am talking about drawing a flow diagram with pen and paper (remember those??), and then coding the flow diagram. By making each routine as small as possible, ie, one job per routine, you simplify your logic because at a high level, you get code that looks like this:

private sub MovePiece()
IsThisAValidMove()
MovePieceToLocation(x,y)
IsPlayerInCheck()
HasPieceTakenOpponent()
End Sub

This is NOT actual code from my source, but it illustrates the type of things I was trying to achieve (as I mentioned before, this was one of my first, so I might not have succeeded entirely).

3) Move highlighting. Because of the modular style of writing this software as described in the point above, it was very easy for me to add “move highlighting”. For the colour whose turn it is to move, right-click on any piece, and it highlights all the possible moves that piece can make at that time, so this could potentially help someone to learn the game.

4) There is no AI in this game yet. This game currently requires 2 people to play. There has not been any effort yet to add artificial intelligence for a computer player.

PLEASE, IF YOU START THE PROCESS OF ADDING AI TO THIS GAME, I’D LOVE TO SEE IT.

If you use this code as the start of your own project (with or without AI), PLEASE send it back to me. I will NEVER ask you for anything, ie, I don’t want your money, I don’t want royalties, I don’t even want acknowledgement in your product (although it would be nice). ALL I want is just to see where this code is going, and please, if you find this useful, please tweet about it @softsmart!

Anyway, having said that, please download the code and happy coding / gaming….

Visual Basic Chess Game Source Code –

 


 

Get Adobe Flash playerPlugin by wpburn.com wordpress themes

 

 

We accept PayPal

 

Web Hosting by PHP-Web-Host.com Website Hosting by PHP-Web-Host.com

[ Home ] | [ Software ] | [ Web Development ] | [ Resources ] | [ Contact ] | [ Privacy Policy ] | [ Acceptable Use Policy ] | [ Terms of Service ] | [ Sitemap ] | [ Clients ] | [ Affiliates ]

[ PHP Web Hosting ] | [ Linux Web Hosting ] | [ 500mb PHP Web Hosting ] | [ 1Gb PHP Web Hosting ] | [ 3Gb PHP Web Hosting ] | [ 6Gb PHP Web Hosting ] | [ 10Gb PHP Web Hosting ]

[ HTML Tutor ] | [ CSS Tutorial ] |