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 ‘C++’ 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!

 


 

Converting Links in a text file to anchor tags usable in your website

Saturday, January 9th, 2010

I recently answered a question about converting links in a text file, eg, www.softsmart.co.za to an anchor link that could be used in a website, eg, <a href=”http://www.softsmart.co.za”>http://www.softsmart.co.za</a>.

I decided that since I was such an open source evangelist, the best thing I could do was whip up a little application to do this:

Here is a brief synopsis of what it does (links at the bottom):
It takes a text file that has one link per line.

You can start the program in several ways (its a console program):
1) Just start it and it will ask you to type in the full path and name of the file (unless the file is in the same directory, then name alone should be fine)
2) You can start the program from a dos prompt followed by the name of the file in quotes, eg, c:\>UrlToAnchor.exe “C:\Folder 1\Folder 2\test.txt”
3) Easiest method is open the folder that has this exe, then in a different folder open the folder with your text file, then just drag the text file onto the program.

It will ask you 3 questions. These are related to what you want the resultant anchor text to look like:

1) Do you want a title tag? If ‘y’ then it will place the title=”eee” into the tag, using the domain name as the title
2) Do you want to use the title tag as the link text? If ‘y’ then the title tag is the link text, ie, what the user sees and clicks on
3) Do you want the links to open in a new window? If ‘y’ it adds the target=”_new”. This is nice if you are placing these links on your own site but you don’t want people to go out of your site.

I tested this on a very average machine (my laptop) with a file that had 200 000 dummy links. It ran that in just under 2 minutes.

Ok, here is the disclaimer…

1) I am giving this away for free, so there are no guarantees. However, if you need help or you want a feature added, mail me, I’d be more than happy to help. My time is very limited however so you may have to wait.

2) I know that its scary using free software from someone you don’t know, so I’ll make this opensource. You can download the program and run it, or you can download the source and check it out, modify it then compile it yourself. Although I haven’t tested this on Linux yet, I do try to stick to ANSI C++, so this should compile on both (Please post here if you want me to compile a Linux version).

What do I ask in return? If you want to make a comment on the blog, make it nice! If you do download the source and modify it (or post it on your own website), please respect 2 things:

1) Please post back to my blog to let us all benefit from the mods
2) Please keep my title info at the top of the source file (its only one file). You can add yours below!

Click here to download the exe for windows

Click here to download the source file

 


 

Absolute beginners guide to compiling dll’s with g++

Thursday, January 7th, 2010

Using the gcc / g++ compiler is really great for a few reasons. One of these reasons is its open source, so it cost you no money!!! What a great price.

A second reason is that it works across various operating systems. We use it equally on both Linux and Windows (any guess which one we prefer???).

We’re going to give you a quick, dirty guide to compiling and linking a simple dll using the g++ compiler and writing in C++. Please note that this really is a basic example (did you think we were kidding when we called it the ABSOLUTE beginners guide???).

Right, firstly we need three files for this example:

  1. testdll.H
  2. testdll.cpp
  3. testprog.cpp

The code in the dll header file should look as follows:

/*——————————————————————————————

testdll.H

——————————————————————————————-*/
#include <stdio>

class SomeClass
{
public:

int hello();

};

The next file, testdll.cpp looks like this:

/*——————————————————————————————

testdll.cpp

——————————————————————————————-*/
#include “
testdll.H”

int SomeClass::hello()
{
printf (“Hello World!\n”);
return 0;
}

and then lastly, the main program page looks like this:

/*——————————————————————————————

testprog.cpp

——————————————————————————————-*/
#include “
testdll.H”
using namespace std;

int main ()
{
SomeClass c;
c.hello ();
}

Now to do the compiling. We are assuming that all of these files are in the same directory and that you are compiling from the command line in Linux using a standard terminal or from MinGW (also open source) using Windows.

  1. g++ -c testdll.cpp
    • This results in a file called testdll.o
  2. ar -rv libtestdll.a testdll.o
    • Create a shared library called libtestdll.a from the object we just created
  3. g++ -o testprog testprog.cpp -L ./ -ltestdll

If all went well you should now be able to run the program using ./testprog

This exact program has been tested on both Windows (XP) and Linux (Mandriva 2009.1).

NOTE:

  • The .cpp and .H files need a carriage return at the end of the file. So after the last line of code, hit enter, then save.
  • In step 2 of the compilation process you created a library called libtestdll.a. In step 3 you used that lib with the -ltestdll option. The option to link is -l and the library name is testdll. You do not call the library libtestdll in this option because the compiler assumes the lib part… Make sense?

for  a far more in depth  look at the gcc compiler, take a look at Phoxis blog.

Well, that’s it for our not-so-earth-shattering, earth shattering tutorial. We hope it helps. If you have any questions about this, or if you require any custom software work, give us a call, we’re always happy to help!

John

 


 

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 ] |