some of the windows dll contain API functions and from my understanding API's are written in C++...
could someone elaborate on this process?
Printable View
some of the windows dll contain API functions and from my understanding API's are written in C++...
could someone elaborate on this process?
Yeah, I'll begin, although there are probably other people with an idea.
Most of the DLLs are presumably written in C. Some might have been written in other languages, but C/C++ is an execellent language for writing DLL files.
Normally, I write most of my software in VB, because it is, well EASY. However, when you need some fast routines, it makes sense to go through the extra work of writing it in C or assembly.
You can easily write your own DLLs if you have a C compiler. I'll use MS Visual C++ for this example.
The first thing you need to do is to create a new project in VC. The type of this project should be a "Win32 Dynamic Link Library".
The next thing is to create two file, a .cpp file and .def file. The DEF file will contain the functions you wish to "export", i.e. make visible to the outside world, include VB.
In the .cpp file, you will need the following as a minimum:
In this case, the DllMain function simply returns TRUE no matter what. In real life, you would probably take action depending on the value of the ul_reason_for_call parameter.Code:#include <windows.h>
/*
This is the main function for the DLL. All Win32 DLLS
should have this.
*/
BOOL APIENTRY DllMain(HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved)
{
return TRUE;
}
The next thing you need is to write a function that can be called from the outside. This is just like writing any other C function apart from one thing. You insert the WINAPI macro (I think it is a macro) before the function name, like this:
Now, all that remains is to export the function using the definition file (.def).Code:/*
Replaces all spaces by underscore.
IN/OUT: *pString The string to process
IN: nLength The string length (in characters).
*/
void WINAPI SpaceToUnderscore(unsigned char *pString,
signed long nLength)
{
for (long i = 0; i < nLength; i++)
{
if (*(pString + i) == ' ') *(pString + i) = '_';
}
}
The uppercase words are always the same. After LIBRARY comes the name of your DLL. In this example, it is a DLL called StringVBC.dll. There is also a DESCRIPTION field, where you can add a description.Code:LIBRARY StringVBC
DESCRIPTION "String VB/C"
EXPORTS
SpaceToUnderscore @1
The EXPORTS section lists all the function names, you wish to export (make visible) followed by @1 for the first function, @2 for the next function, etc. This might look like this, had there been more functions:
Once all these things are in place, compile the DLL. If you get no errors (hopefully), then you're ready to proceed to the VB part.Code:LIBRARY StringVBC
DESCRIPTION "String VB/C"
EXPORTS
SpaceToUnderscore @1
MyFunction @2
YourFunction @3
WhateverFunction @4
You need to declare the DLL in VB, just like all other API functions. In our example, we assume the DLL is called StringVBC.dll, so to call the SpaceToUnderscore function, you declare it like this:
VB Code:
Private Declare Sub SpaceToUnderscore Lib "stringvbc.dll" _ (ByVal pString As String, ByVal argLength As Long)
Then, all you need to do is to call the function like any other function.
VB Code:
Dim str As String Let str = "Hey how are you?" SpaceToUnderscore str, Len(str) 'call C function Debug.Print str
It is worth pointing out, that strings should be passed from VB as ByVal and so should Long values. I am not exactly sure why this is.
IMPORTANT Your DLL must reside somewhere on the disk where VB can find it. This is typically in the system directory or Windows directory, but it could also be the program directory.
I hope this helps you.
yeah that was pretty cool! thanks a lot :-) just one more question though-- i am learning assembly language-- what program do you use to make a .dll from it? can visual studio do it?
The .NET environment supports a kind of assembler (IL) and it can be used to do literally anything. Considering that it is a low-level kind of environment that supports classes, it really does allow for good development. You can make a dll from it.
Windows devlopment in conventional ASM (like MASM32) is very painful and slow. You can make a dll from this as well. See the assembler forum here. It has links to advanced programming in assembler.
OK, glad you liked it :)
In Visual Studio 5/6 (and probably earlier versions) you can use the __asm keyword to insert assembly statements directly into a C function.
You can also use variables in this type of assembly code. It is a pretty cool feature, although that .NET thing, jim mcnamara talks about sounds cool.Code:void WINAPI MyFunc() {
/*
Insert some assembly lines here
*/
__asm {
xor eax,eax ; whatever ASM statements here
xor ebx,ebx
}
}
that was nice lil example man... thanks for gettin me started wit VC++ dll's....Quote:
Originally posted by HaxSoft
Yeah, I'll begin, although there are probably other people with an idea............
can u suggest me any book or web site or MSDN page for more info on creating dll's??
I am sorry for not replying earlier, but for what it's worth; here is my reply now.
I got the information on DLL files from the documentation that accompanies Microsoft Visual C++. I imagine that the documentation would be available at Microsoft's web site (msdn.microsoft.com).
As always, finding info on the Microsoft web site is like speaking Spanish to a dog. Even a Spanish dog would not respond with anything that remotely matches what you was looking for.
So, the answer to your question is; "The only info I have on DLLs is the MSVC 5.0 documentation".
Nothing (constructive) to add to the above, just amazed to encounter 2 references to Faroe Islands in one day - I just heard on the radio that there is a Faroe Islands football team competing in Euro-2004 - then I spot HaxSoft's location! - they've got a football team and at least one VB nut!!
Cheers!
DrMemory
but if this , how to do ???????????
char **
void WINAPI SpaceToUnderscore(unsigned char **pString)
{
// do somthing;
}
Quote:
Originally posted by MathImagics
Nothing (constructive) to add to the above, just amazed to encounter 2 references to Faroe Islands in one day - I just heard on the radio that there is a Faroe Islands football team competing in Euro-2004 - then I spot HaxSoft's location! - they've got a football team and at least one VB nut!!
Cheers!
DrMemory
Thanks for the nice words ... I think "VB NUT" is the most beautiful thing anyone has ever called me :-)
Anyway, there are more than on VB NUT in the Faroes, but I must just be the most NUTS one, haha.
Cheers
Well, I am not sure. I am not the C++ expert ... and besides, I am going on vacation tomorrow, so I need to spend my time packing rather than hacking.Quote:
Originally posted by gyang
but if this , how to do ???????????
char **
void WINAPI SpaceToUnderscore(unsigned char **pString)
{
// do somthing;
}
However, I am sure that someone like Megatron would know. If not, then I'll be happy to figure it out when I get back. That won't be for another 4 weeks, though. I hope you can wait that long.
Was this question 'of of need' or 'out of interest'?