Jump to content

C++ DLL Examples Please


jasperhale
 Share

Recommended Posts

Hello,

I want to learn how to write C++ Code.

Can anyone please show me some examples in C++ ? I want to make some dll by Visual C++ but i dont know C++ at all.

Just search in google and see something.
 

;Example.h
int Add(int a, int b);
; Example.cpp
#include "stdafx.h"

long _stdcall Add( int a, int b )
        {
            return a + b;
        }
; example.def
LIBRARY "example"

EXPORTS
Add @1

 

 

I want more examples please, just some basic function.

If anyone know VC++ DLL please help me some examples :

- AutoIT send some text to DLL, DLL process and return some text....

- AutoIT send EXE path to DLL and DLL open it.

 

Please show source so i can learn from it.

Thanks you verry much.

Edited by jasperhale
Link to comment
Share on other sites

56 minutes ago, AutoBert said:

Did you realized, where you are posting? "AutoIt General Help and Support"! I suggest ask in a C++ Forum.

No, you dont understand.

If i post in C++ Forum, they never use AUTOIT, so they dont have right examples.

I want C++ examples to build DLL files that use in AutoIT.

 

Link to comment
Share on other sites

@moderators: Please move this thread to AutoIt's C++ forum (AutoBert is right that it doesn't belong in General Support, but it doesn't belong in C#/.NET either!).

@jasperhale:I think AutoBert meant moving your post to AutoIt's C++ sub forum.:) There you'll likely find simple dll implementation examples. For a more advanced example, see my Eigen4AutoIt wrapper library (link in my sig; includes source example for one of the dlls I wrote for that package).

Edited by RTFC
Link to comment
Share on other sites

  • Moderators

Sorry, finger trouble for the first move.

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

18 minutes ago, RTFC said:

@moderators: Please move this thread to AutoIt's C++ forum (AutoBert is right that it doesn't belong in General Support, but it doesn't belong in C#/.NET either!).

@jasperhale:I think AutoBert meant moving your post to AutoIt's C++ sub forum.:) There you'll likely find simple dll implementation examples. For a more advanced example, see my Eigen4AutoIt wrapper library (link in my sig; includes source example for one of the dlls I wrote for that package).

Ok i will download and see it, thanks you verry much. If you have any basic c++ examples, please give to me, i'm too bad in C++.

 

Edited : I cant download Eigen4AutoIT, when downloaded, and unrar, its come with error, can you please reupload it for me ???

Edited by jasperhale
Link to comment
Share on other sites

@Melba23: no worries, mate.

@jasperhale: I just tested that the Eigen4AutoIt archive contains no errors. You need to unzip it with 7-zip though, not unrar it.;) Re simpler examples, please search AutoIt's C++ forum, as I suggested earlier (I am not going to hold your hand every step of the way; show us what you've tried, and we may be able to solve specific issues you're having, or suggest why it might not be working as expected:)).

NB you may wish to consult monoceres's dll Tutorial as well.

Link to comment
Share on other sites

@jasperhale I'm not sure how to do what you're looking for specifically, but I've linked some posts that may help you on your search. Hopefully, these will give you a better understanding on how to use AutoIt with DLLs

This is a post I did that allowed me to Login as another user and Impersonate them using DLLs

 

Link to comment
Share on other sites

43 minutes ago, RTFC said:

@Melba23: no worries, mate.

@jasperhale: I just tested that the Eigen4AutoIt archive contains no errors. You need to unzip it with 7-zip though, not unrar it.;) Re simpler examples, please search AutoIt's C++ forum, as I suggested earlier (I am not going to hold your hand every step of the way; show us what you've tried, and we may be able to solve specific issues you're having, or suggest why it might not be working as expected:)).

NB you may wish to consult monoceres's dll Tutorial as well.

@RTFC Thanks You verry much, i have install 7-zip, and i can unzip good.

But maybe you and @Surf243 were wrong.

I dont want to learn how to use DLL in AutoIT, i want to learn how to write C++, and build to DLL.

I use DLL Call in AutoIt many times, so i think i can do good in AutoIT.

But i dont know how to write C++ code, and build to DLL. I dont know at all.

I just have some basic examples but i dont have more, if you have C++ Code Examples, please share with me, i will learn from it.

Thanks you verry much again.

Link to comment
Share on other sites

I understand you quite well, jasperhale.:huh: If you were to examine subdir .\Eigen4AutoIt\source, you'll find EigenDense.cpp (the C++ source code I wrote) and EigenDense.h (the associated header file). These contain the C++ functions called in Eigen4AutoIt.au3 (the AutoIt wrapper library). But I realise this is probably a bit complicated if you've never written a dll before. So just this once I'll provide you with a really basic example.

dlltest.h (header file)

#ifndef _DLLTEST_H_
#define _DLLTEST_H_  // This is basically to tell compiler that we want to include this code only once(in case duplication occurs)

#define DLL_EXPORT
#if defined DLL_EXPORT
  #define DECLDIR __declspec(dllexport)
#else
  #define DECLDIR __declspec(dllimport)
#endif

extern "C"
{
    // declare all functions here
   DECLDIR void Function( void );
   DECLDIR int Add( int a, int b ); 
   
}
#endif

 

dlltest.cpp (C++ source)

#include <iostream>
#include <Windows.h> // this is where MessageBox function resides(Autoit also uses this function)
#include "dlltest.h" // We include our header file which contains function declarations and other instructions

using namespace std;

extern "C"
{

   DECLDIR int Add( int a, int b )
   {
      return( a + b );
   }

   DECLDIR void Function( void )
   {
      std::cout << "DLL Called! Hello AutoIt!" << std::endl; // This silly function will just output in our Autoit script "Dll Called!"
      MessageBox(0, TEXT("DLL from Autoit"), TEXT("Simple Dll..."),0);  // Use good old Message Box inside Windows Api Functions but this time with your own dll }
    }

}

Of course, you need to compile this as a dll, not as an executable! As you can tell from the code below, I'm using MSVC2010 here, but any decent C++ compiler will do.

Finally, the AutoIt script calling this dll:

$dll = DllOpen("<fill in your drive+directory structure here>\Visual Studio 2010\Projects\dlltest\Debug\dlltest.dll")

DllCall($dll, "none", "Function")

$test = DllCall($dll, "int:cdecl", "Add", "int", 2, "int", 3)
MsgBox(0,"Error: " & @error,$test[0])

DllClose($dll)

This script calls a C++ function called Function, which returns nothing (void), but calls a Windows Messagebox, and writes some text to Console. It then calls the C++ function Add with two parsed integers (2 and 3), returning their sum (also as an integer). Once everything works as expected, we can compile the Release version (without extra error handling and padding), and copy the dll to wherever we need it.

All clear now?

Edited by RTFC
Link to comment
Share on other sites

9 hours ago, RTFC said:

I understand you quite well, jasperhale.:huh: If you were to examine subdir .\Eigen4AutoIt\source, you'll find EigenDense.cpp (the C++ source code I wrote) and EigenDense.h (the associated header file). These contain the C++ functions called in Eigen4AutoIt.au3 (the AutoIt wrapper library).

All clear now?

 

Yes, thanks you verry much.

I can see the source cpp from EigenDense.cpp but as you said, i dont know C++ at all, that 's why i cant understand it.

This examples is good, i love it.

Thanks you verry much.

Link to comment
Share on other sites

  • 10 months later...

Hi, I want to create a dll file to use it in my autoit project. but I take an error when I run autoit script. I create a Win32 console app project named TestDLL by using Microsoft Visual Studio 2012. I selected DLL type application in app settings. Then I did my cpp codes and build it to create dll file. I added my codes for this project below. How can I handle this problem? Thank you.

TestDLL.cpp

// TestDLL.cpp : Defines the exported functions for the DLL application.
//

#include "stdafx.h"

extern "C" __declspec(dllexport) int Add(int a, int b);

int Add(int a, int b){
    return a+b;
}

dllmain.cpp 

// dllmain.cpp : Defines the entry point for the DLL application.
#include "stdafx.h"

BOOL APIENTRY DllMain( HMODULE hModule,
                       DWORD  ul_reason_for_call,
                       LPVOID lpReserved
                     )
{
    switch (ul_reason_for_call)
    {
    case DLL_PROCESS_ATTACH:
    case DLL_THREAD_ATTACH:
    case DLL_THREAD_DETACH:
    case DLL_PROCESS_DETACH:
        break;
    }
    return TRUE;
}

Autoit script

#include <MsgBoxConstants.au3>

$DllName ="E:\Autoit\TestDLL.dll"

$hDLL = DllOpen($DllName)

$Result = DllCall($hDLL,"int:cdecl","Add","int",5,"int",4)
If Not @error Then
    MsgBox($MB_SYSTEMMODAL, "Result", $Result[0])
Else
    MsgBox($MB_SYSTEMMODAL, "Error", @error)
EndIf

DllClose($hDLL)

Error message

error.PNG

Edited by blue_screen
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...