Help With making DLL in vc++

  • Joshay
  • Newbie
  • Newbie
  • No Avatar
  • Joined: Aug 08, 2005
  • Posts: 13
  • Status: Offline

Post August 8th, 2005, 4:37 pm

Well i was told i have to change the exported functions to stdcall. And im calling the dll functions from vb. Here is the code. Can someone tell me how to set upt his project and chnage the exported functions to stdcall?
The code is at http://www.winmxunlimited.net/mxsock.h. Please Help.
  • Anonymous
  • Bot
  • No Avatar
  • Joined: 25 Feb 2008
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post August 8th, 2005, 4:37 pm

  • Carl
  • Beginner
  • Beginner
  • User avatar
  • Joined: Jul 20, 2004
  • Posts: 42
  • Status: Offline

Post August 8th, 2005, 7:49 pm

In addition to changing the functions to stdcall, you also need to export the functions. You may also need to use extern "C" for the functions to work from VB. Also, does your DLL have an entry point?

Add the following definition in the files where you want your functions to be visible:
Code: [ Download ] [ Select ]
#define WINAPI __stdcall
#define DllExport extern "C" __declspec(dllexport)
  1. #define WINAPI __stdcall
  2. #define DllExport extern "C" __declspec(dllexport)


A function definition using this would then become:
Code: [ Download ] [ Select ]
DllExport int WINAPI SomeFunction()
{
// ... code ...
}
  1. DllExport int WINAPI SomeFunction()
  2. {
  3. // ... code ...
  4. }


Somewhere in your DLL you also need a function that looks similar to this:
Code: [ Download ] [ Select ]
BOOL WINAPI DllMain(HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
{
    switch(ul_reason_for_call)
    {
        case DLL_PROCESS_ATTACH:
        {
// ... do something ...
            break;
        }
        case DLL_PROCESS_DETACH:
        case DLL_THREAD_ATTACH:
        case DLL_THREAD_DETACH:
            break;
    }
    return TRUE;
}
  1. BOOL WINAPI DllMain(HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
  2. {
  3.     switch(ul_reason_for_call)
  4.     {
  5.         case DLL_PROCESS_ATTACH:
  6.         {
  7. // ... do something ...
  8.             break;
  9.         }
  10.         case DLL_PROCESS_DETACH:
  11.         case DLL_THREAD_ATTACH:
  12.         case DLL_THREAD_DETACH:
  13.             break;
  14.     }
  15.     return TRUE;
  16. }


Unless you give more specific information, I'm afraid this is all I can do to help you.
  • Joshay
  • Newbie
  • Newbie
  • No Avatar
  • Joined: Aug 08, 2005
  • Posts: 13
  • Status: Offline

Post August 8th, 2005, 10:22 pm

well carl when i put the code in the palce wher eit shows it gives me lots of errors? like k and byte is undefiened and those sorts of things but in the header it works fine? I just need these functions to be in a dll and for me to access them from vb? Although the function inline thing Encrypt1 and Decrypt1 are used internaly and not exported functions? if i were to call the m from vb it would be like this right?

Code: [ Download ] [ Select ]
Public Declare Function DecryptFrontCode Lib "mxsock.dll" (ByRef pSrc As Byte, ByRef pDst As Byte) As Long
Public Declare Function CreateCryptKeyID Lib "mxsock.dll" (ByVal wID As Integer, ByRef pBlock As Byte) As Long
Public Declare Function GetCryptKeyID Lib "mxsock.dll" (ByRef pBlock As Byte) As Long
Public Declare Function GetCryptKey Lib "mxsock.dll" (ByRef pBlock As Byte, ByRef pUpKey As Long, ByRef pDwKey As Long) As Long
Public Declare Function DecryptMXTCP Lib "mxsock.dll" (ByRef pBuf As Byte, ByVal iLen As Integer, ByVal dwKey As Long) As Long
Public Declare Function EncryptMXTCP Lib "mxsock.dll" (ByRef pBuf As Byte, ByVal iLen As Integer, ByVal dwKey As Long) As Long
Public Declare Function DecryptMXUDP Lib "mxsock.dll" (ByRef pBuf As Byte, ByVal iLen As Integer) As Long
Public Declare Function GetFileHash Lib "mxsock.dll" (ByVal lpszFileName As String, ByRef pHash As Long, ByRef pFileLen As Long) As Long
Public Declare Function EncryptMXUDP Lib "mxsock.dll" (ByRef pBuf As Byte, ByVal iLen As Integer) As Long
  1. Public Declare Function DecryptFrontCode Lib "mxsock.dll" (ByRef pSrc As Byte, ByRef pDst As Byte) As Long
  2. Public Declare Function CreateCryptKeyID Lib "mxsock.dll" (ByVal wID As Integer, ByRef pBlock As Byte) As Long
  3. Public Declare Function GetCryptKeyID Lib "mxsock.dll" (ByRef pBlock As Byte) As Long
  4. Public Declare Function GetCryptKey Lib "mxsock.dll" (ByRef pBlock As Byte, ByRef pUpKey As Long, ByRef pDwKey As Long) As Long
  5. Public Declare Function DecryptMXTCP Lib "mxsock.dll" (ByRef pBuf As Byte, ByVal iLen As Integer, ByVal dwKey As Long) As Long
  6. Public Declare Function EncryptMXTCP Lib "mxsock.dll" (ByRef pBuf As Byte, ByVal iLen As Integer, ByVal dwKey As Long) As Long
  7. Public Declare Function DecryptMXUDP Lib "mxsock.dll" (ByRef pBuf As Byte, ByVal iLen As Integer) As Long
  8. Public Declare Function GetFileHash Lib "mxsock.dll" (ByVal lpszFileName As String, ByRef pHash As Long, ByRef pFileLen As Long) As Long
  9. Public Declare Function EncryptMXUDP Lib "mxsock.dll" (ByRef pBuf As Byte, ByVal iLen As Integer) As Long
  • Carl
  • Beginner
  • Beginner
  • User avatar
  • Joined: Jul 20, 2004
  • Posts: 42
  • Status: Offline

Post August 9th, 2005, 12:40 am

If possible, please post a link to your current source code or post samples. I'm really not clear where you placed my sample code or what it is you are ultimately trying to accomplish. I'm afraid I can't help unless you give more information.
  • Joshay
  • Newbie
  • Newbie
  • No Avatar
  • Joined: Aug 08, 2005
  • Posts: 13
  • Status: Offline

Post August 9th, 2005, 1:55 pm

ok visit http://www.winmxunlimited.net/code.zip
  • Carl
  • Beginner
  • Beginner
  • User avatar
  • Joined: Jul 20, 2004
  • Posts: 42
  • Status: Offline

Post August 9th, 2005, 2:47 pm

Okay first of all, you don't have a DLL entry point or any sort of main() function. A C/C++ program REQUIRES a main() function. Since you are writing a Windows API DLL, it's called DllMain() (see below). Additionally, you aren't including windows.h anywhere in your program (which is a HUGE problem). One of the first rules of Windows API programming is include the following statement:
Code: [ Download ] [ Select ]
#include <windows.h>

There are many other problems with your code but I really don't have time to teach you Windows API or DLL programming. I highly recommend learning C/C++ before proceeding. After you've learned C/C++ then I suggest looking into getting a Windows API book that teaches how to write DLLs. Another option would be to directly translate all the code into VB or C# and use the .NET Framework.

Code: [ Download ] [ Select ]
BOOL WINAPI DllMain(HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
{
// code
  return TRUE;
}
  1. BOOL WINAPI DllMain(HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
  2. {
  3. // code
  4.   return TRUE;
  5. }
  • Joshay
  • Newbie
  • Newbie
  • No Avatar
  • Joined: Aug 08, 2005
  • Posts: 13
  • Status: Offline

Post August 9th, 2005, 5:29 pm

can you help me turn it into vb?
  • Carl
  • Beginner
  • Beginner
  • User avatar
  • Joined: Jul 20, 2004
  • Posts: 42
  • Status: Offline

Post August 9th, 2005, 9:21 pm

I'm afraid I don't have that kind of time. Sorry.
  • Joshay
  • Newbie
  • Newbie
  • No Avatar
  • Joined: Aug 08, 2005
  • Posts: 13
  • Status: Offline

Post August 10th, 2005, 1:37 pm

well can you just help me with this dll. i just need to make a dll that these functions can be called from vb and then they work. but i was told they have to be int he std thing which u helped but how exactly?
  • Carl
  • Beginner
  • Beginner
  • User avatar
  • Joined: Jul 20, 2004
  • Posts: 42
  • Status: Offline

Post August 12th, 2005, 1:49 pm

First of all, I'm not going to write the DLL for you. You won't learn anything that way and I don't have the time to take on another project at the moment. If getting the DLL to work in VB is too much work, then I suggest you find a language other than VB to write your program in. C code translates to C# code quite nicely (well, for the most part) and it's really easy to create a GUI using C#. You may want to look into that.

Secondly, I've already showed you how to call functions using __stdcall. After defining the macro I showed you, place WINAPI between the function return type and the function name. To make a function accessible, it needs to be exported. You can specify which functions are to be exported by placing __declspec(dllexport) in front of a function return type. You may need to use extern "C" in from of __declspec(dllexport) to get the function to work from VB. Once you get the DLL to compile, try it without and see if the functions are defined. If they aren't try adding extern "C" before __declspec(dllexport). I defined a macro for you that can use instead of __declpec(dllexport) to help make your code a bit more readable.

If you are still lost, I suggest learning C/WinAPI programming. Once you know a little bit about C/WinAPI programming, this DLL stuff shouldn't be too difficult. Not only that but you'll know another programming language. :)
  • Joshay
  • Newbie
  • Newbie
  • No Avatar
  • Joined: Aug 08, 2005
  • Posts: 13
  • Status: Offline

Post August 17th, 2005, 12:09 am

ok i think i have it all figured out but i need one last help!

Code: [ Download ] [ Select ]
#include <windows.h>
#include <stdio.h>
#include "wpnencryptiontables.h"
#define WINAPI __stdcall
#define DllExport extern "C" __declspec(dllexport)

BOOL WINAPI DllMain(
 HINSTANCE hinstDLL,
 DWORD fdwReason,
 LPVOID lpvReserved
);
DllExport BOOL WINAPI GetFileHash(LPCTSTR lpszFileName, DWORD *pHash, DWORD *pFileLen)
{
    FILE *in;
    DWORD len;
    DWORD blockskip;

    if((in = fopen(lpszFileName, "rb")) == NULL) return FALSE;

    BYTE buf[0x20000];

    fseek(in, 0 , SEEK_END);

    *pFileLen = ftell(in);

    fseek(in, 0, SEEK_SET);

    blockskip = *pFileLen / 0x20000 / 10;
    if(blockskip < 5) blockskip = 5;

    pHash[0] = pHash[1] = pHash[2] = pHash[3] = 0;

    len = fread(buf, 1, 0x20000, in);

    while(1)
    {
        MXHashSub(buf, len, (BYTE *)pHash);
        if(len < 0x20000) break;

        fseek(in, 0x20000 * (blockskip - 1), SEEK_CUR);

        len = fread(buf, 1, 0x20000, in);
    }

    fclose(in);

    return TRUE;
}
  1. #include <windows.h>
  2. #include <stdio.h>
  3. #include "wpnencryptiontables.h"
  4. #define WINAPI __stdcall
  5. #define DllExport extern "C" __declspec(dllexport)
  6. BOOL WINAPI DllMain(
  7.  HINSTANCE hinstDLL,
  8.  DWORD fdwReason,
  9.  LPVOID lpvReserved
  10. );
  11. DllExport BOOL WINAPI GetFileHash(LPCTSTR lpszFileName, DWORD *pHash, DWORD *pFileLen)
  12. {
  13.     FILE *in;
  14.     DWORD len;
  15.     DWORD blockskip;
  16.     if((in = fopen(lpszFileName, "rb")) == NULL) return FALSE;
  17.     BYTE buf[0x20000];
  18.     fseek(in, 0 , SEEK_END);
  19.     *pFileLen = ftell(in);
  20.     fseek(in, 0, SEEK_SET);
  21.     blockskip = *pFileLen / 0x20000 / 10;
  22.     if(blockskip < 5) blockskip = 5;
  23.     pHash[0] = pHash[1] = pHash[2] = pHash[3] = 0;
  24.     len = fread(buf, 1, 0x20000, in);
  25.     while(1)
  26.     {
  27.         MXHashSub(buf, len, (BYTE *)pHash);
  28.         if(len < 0x20000) break;
  29.         fseek(in, 0x20000 * (blockskip - 1), SEEK_CUR);
  30.         len = fread(buf, 1, 0x20000, in);
  31.     }
  32.     fclose(in);
  33.     return TRUE;
  34. }

i have that c++ project with the mxhashsub function too but i didn't show that. now when i call it in vb in a module i do this
Code: [ Download ] [ Select ]
Public Declare Function GetFileHash Lib "MXSck.dll" (ByVal lpszFileName As String, ByRef pHash As Long, ByRef pFileLen As Long) As Boolean


now on a button i have
Code: [ Download ] [ Select ]
Dim p As Long
Dim pf As Long
GetFileHash "C:\Documents and Settings\Josh\Desktop\music\Evolution.avi", p, pf
  1. Dim p As Long
  2. Dim pf As Long
  3. GetFileHash "C:\Documents and Settings\Josh\Desktop\music\Evolution.avi", p, pf

and i get an error saying
"Cant Find Dll Entry Point GetFileHash in mxsck.dll"

Any ideas carl?
  • Carl
  • Beginner
  • Beginner
  • User avatar
  • Joined: Jul 20, 2004
  • Posts: 42
  • Status: Offline

Post August 17th, 2005, 6:17 am

There isn't a boolean data type in C. BOOL is a type defintion in the Windows API and is actually an integer type. The standard convention is as follows:
Code: [ Download ] [ Select ]
typedef int BOOL;
#define TRUE 1
#define FALSE 0
  1. typedef int BOOL;
  2. #define TRUE 1
  3. #define FALSE 0

Since your return type in VB doesn't match the return type of the actual function, Visual Studio can't find the function definition. Try the following VB function declaration:
Code: [ Download ] [ Select ]
Public Declare Function GetFileHash Lib "MXSck.dll" (ByVal lpszFileName As String, ByRef pHash As Long, ByRef pFileLen As Long) As Integer

More information on Windows API data types can be found here:
Windows API Data Types

DllMain also needs to return a value. As you currently have it, DllMain doesn't do anything.
Code: [ Download ] [ Select ]
BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
{
  // do something
  return TRUE;
}
  1. BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
  2. {
  3.   // do something
  4.   return TRUE;
  5. }

Since it has a BOOL return type, it has to return TRUE or FALSE.

If you still have problems, make sure that your DLL compiles without errors. If it doesn't, Visual Studio won't create an output file, which will cause all sorts of problems in your Visual Basic program.
  • Joshay
  • Newbie
  • Newbie
  • No Avatar
  • Joined: Aug 08, 2005
  • Posts: 13
  • Status: Offline

Post August 17th, 2005, 12:55 pm

still it didn't work. Can you please tell me wuts wrong. The c++ and vb code is here. - http://www.winmxunlimited.net/mxsck.zip
  • Carl
  • Beginner
  • Beginner
  • User avatar
  • Joined: Jul 20, 2004
  • Posts: 42
  • Status: Offline

Post August 17th, 2005, 1:58 pm

First of all, you didn't copy your compiled DLL into your VB project folder. Second, your DLL is incorrectly named in all of your function definitions. Your DLL is called "mxsck.dll" but in your function definitions you call it "MXSock.dll" and "MXSck.dll." Those aren't the same thing.

You may also want to try removing extern "C" from the DllExport macro.

Open up Project1.vdp and change the line that starts with "Module=MXSock; ..." to
Code: [ Download ] [ Select ]
Module=MXSock; MXSock.bas


As far as I can tell your VB code is correct, although I'm not a VB programmer. So, I don't know the specifics of importing a DLL into regular VB. I suggest switching over to C# or VB.NET since both languages make it a lot easier to import DLLs and are designed to combine multiple languages.

If you don't have C# support in Visual Studio, I recommend downloading and installing #develop which is an open source development environment for the .NET Framework. Learning C# or VB.NET will make your life a lot easier and will probably take less time than messing with VB.

If you prefer to stick with VB, then you may want to look at this page:
http://www.flipcode.com/articles/article_vbdlls.shtml
  • Joshay
  • Newbie
  • Newbie
  • No Avatar
  • Joined: Aug 08, 2005
  • Posts: 13
  • Status: Offline

Post August 17th, 2005, 2:19 pm

i purposly removed the mxsck.dll from the folder so u could build a new one.

and

Open up Project1.vdp and change the line that starts with "Module=MXSock; ..." to
Code:
Module=MXSock; MXSock.bas



what is that supposed to mean?

updated code and i removed the "C" still doesn't work

http://www.winmxunlimited.net/mxsck2.zip
  • Anonymous
  • Bot
  • No Avatar
  • Joined: 25 Feb 2008
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post August 17th, 2005, 2:19 pm

Post Information

  • Total Posts in this topic: 16 posts
  • Users browsing this forum: No registered users and 220 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© Unmelted Enterprises 1998-2009. Driven by phpBB © 2001-2009 phpBB Group.