Results 1 to 11 of 11

Thread: Dll?

  1. #1

    Thread Starter
    Addicted Member MegaMan's Avatar
    Join Date
    May 2002
    Posts
    128

    Question Dll?

    some of the windows dll contain API functions and from my understanding API's are written in C++...

    could someone elaborate on this process?

  2. #2
    Fanatic Member HaxSoft's Avatar
    Join Date
    May 2000
    Location
    Ohio
    Posts
    593
    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:

    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;
    }
    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.

    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:

    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) = '_';
        }
    }
    Now, all that remains is to export the function using the definition file (.def).

    Code:
    LIBRARY StringVBC
    DESCRIPTION "String VB/C"
    EXPORTS
        SpaceToUnderscore    @1
    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.

    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:

    Code:
    LIBRARY StringVBC
    DESCRIPTION "String VB/C"
    EXPORTS
        SpaceToUnderscore    @1
        MyFunction           @2
        YourFunction         @3
        WhateverFunction     @4
    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.

    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:
    1. Private Declare Sub SpaceToUnderscore Lib "stringvbc.dll" _
    2.     (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:
    1. Dim str As String
    2. Let str = "Hey how are you?"
    3. SpaceToUnderscore str, Len(str) 'call C function
    4. 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.

  3. #3

    Thread Starter
    Addicted Member MegaMan's Avatar
    Join Date
    May 2002
    Posts
    128
    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?

  4. #4
    jim mcnamara
    Guest
    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.

  5. #5
    Fanatic Member HaxSoft's Avatar
    Join Date
    May 2000
    Location
    Ohio
    Posts
    593
    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.

    Code:
    void WINAPI MyFunc() {
      /*
        Insert some assembly lines here
      */
      __asm {
        xor eax,eax  ; whatever ASM statements here
        xor ebx,ebx
      }
    }
    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.

  6. #6
    Member
    Join Date
    Apr 2002
    Location
    QLD, AUS
    Posts
    50
    Originally posted by HaxSoft
    Yeah, I'll begin, although there are probably other people with an idea............
    that was nice lil example man... thanks for gettin me started wit VC++ dll's....

    can u suggest me any book or web site or MSDN page for more info on creating dll's??

  7. #7
    Fanatic Member HaxSoft's Avatar
    Join Date
    May 2000
    Location
    Ohio
    Posts
    593
    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".
    Last edited by HaxSoft; Jul 6th, 2002 at 01:24 PM.

  8. #8
    Addicted Member MathImagics's Avatar
    Join Date
    Jun 2002
    Location
    Uki, NSW Australia
    Posts
    169
    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

  9. #9
    New Member
    Join Date
    Jul 2002
    Posts
    11

    if char **,how to do??

    but if this , how to do ???????????

    char **


    void WINAPI SpaceToUnderscore(unsigned char **pString)
    {
    // do somthing;
    }

  10. #10
    Fanatic Member HaxSoft's Avatar
    Join Date
    May 2000
    Location
    Ohio
    Posts
    593
    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

  11. #11
    Fanatic Member HaxSoft's Avatar
    Join Date
    May 2000
    Location
    Ohio
    Posts
    593

    Re: if char **,how to do??

    Originally posted by gyang
    but if this , how to do ???????????

    char **


    void WINAPI SpaceToUnderscore(unsigned char **pString)
    {
    // do somthing;
    }
    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.

    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'?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width