delphino2006 a écrit :
Salut...,
j'utilise une dll ecrite en C++,dans delphi,
type
charp=^char;
var
Form1: TForm;
const
NomDLL = 'DefragDll.dll';
//1° - declaration static:
Function RunDefrag(MountPoint : pchar;Mode:integer):integer;cdecl; external 'DefragDll.dll' name 'RunDefrag';
implementation
{$R *.dfm}
function LierFonction(DLL: String; var HandleDLL: THandle; NomFct: String; IndexFct: Integer = -1): Pointer;
begin
Result := nil;
HandleDLL := 0;
HandleDLL := LoadLibrary(pAnsiChar(DLL));
If HandleDLL = 0 then
Exit;
If IndexFct < 0 then
Result := GetProcAddress(HandleDLL, pAnsiChar(NomFct))
else
Result := GetProcAddress(HandleDLL, pAnsiChar(IndexFct));
end;
2° -declaration denamique.
procedure TForm1.Button1Click(Sender: TObject);
var HandleDLL: THandle;
RunDefrag : function(MountPoint:charp;Mode:integer):integer;cdecl ; //Notre fonction, sous forme de variable.
begin
RunDefrag := LierFonction(NomDLL, HandleDLL, 'RunDefrag');
If assigned(RunDefrag) then
begin
RunDefrag(nil,1);
FreeLibrary(HandleDLL);
end
else
ShowMessage('Erreur de chargement de la fonction "rundefrag"');
end;
//----------------------
//Static procedure .....
procedure TForm1.Button2Click(Sender: TObject);
begin
RunDefrag(nil,1);
end;
mais toujour rien erreur de compilation ;
le code c++ de DLL.------------------------
#define _WIN32_WINNT 0x0500
#include <windows.h>
#include <stdio.h>
#include <time.h>
#include <sys/timeb.h>
#include "Common1.c" /* Includes and variables. */
/* Dll specifics. */
#define DEFRAG_EXPORTS
#include "DefragDll.h"
static PDefragShowMessageCallback ShowMessageCallback;
static PDefragShowMoveCallback ShowMoveCallback;
static PDefragShowAnalyzeCallback ShowAnalyzeCallback;
static PDefragShowDebugCallback ShowDebugCallback;
static PDefragDrawClusterCallback DrawClusterCallback;
static PDefragClearScreenCallback ClearScreenCallback;
static char **DebugMsg;
/* Show a general progress message. */
void ShowMessage(int Message) {
ShowMessageCallback(Message);
}
/* Show progress message during defrag/optimization, about moving a file. */
void ShowMove(char *FileName, DWORD Clusters, ULONG64 FromLcn, ULONG64 ToLcn) {
ShowMoveCallback(FileName,Clusters,FromLcn,ToLcn);
}
/* Show progress message during analyzing. */
void ShowAnalyze(
struct FileListStruct *File,
ULONG64 CountDirectories,
ULONG64 CountAllFiles, ULONG64 CountFragmentedFiles,
ULONG64 CountAllBytes, ULONG64 CountFragmentedBytes,
ULONG64 CountAllClusters, ULONG64 CountFragmentedClusters) {
if (File != NULL) {
ShowAnalyzeCallback(File->FileName,
CountDirectories,
CountAllFiles,CountFragmentedFiles,
CountAllBytes,CountFragmentedBytes,
CountAllClusters,CountFragmentedClusters);
} else {
ShowAnalyzeCallback(NULL,
CountDirectories,
CountAllFiles,CountFragmentedFiles,
CountAllBytes,CountFragmentedBytes,
CountAllClusters,CountFragmentedClusters);
}
}
/* Show a debug message. */
void ShowDebug(int Level, struct FileListStruct *File, char *Message) {
if (File != NULL) {
ShowDebugCallback(Level,File->FileName,Message);
} else {
ShowDebugCallback(Level,NULL,Message);
}
}
/* Paint a cluster on the screen in the color. */
void DrawCluster(ULONG64 ClusterStart, ULONG64 ClusterEnd, int Color) {
DrawClusterCallback(ClusterStart,ClusterEnd,MaxLcn,Color);
}
/* Clear the screen and show the name of the volume. */
void ClearScreen(char *VolumeDescription) {
ClearScreenCallback(VolumeDescription);
}
/* Include the subroutines that are common (equal) in all versions. */
#include "Common2.c"
/* Run the defragger. Execution can be stopped by calling StopDefrag().
If a MountPoint is specified (for example "C:\" ) then only defrag
that disk, otherwise defrag all fixed disks.
Mode:
0 Analyze only
1 Analyze, Defragment
2 Analyze, Defragment, Fast Optimize
3 Analyze, Defragment, Full Optimize
*/
DEFRAG_API int RunDefrag(char *MountPoint, int Mode) {
if ((MountPoint == NULL) || (*MountPoint == '\0')) {
DefragAllDisks(Mode);
} else {
DefragOneDisk(MountPoint,Mode);
}
return(0);
}
/* Stop RunDefrag(). */
DEFRAG_API int StopDefrag(int WaitForIt) {
StopProcessing(WaitForIt);
return(0);
}
/* Stop the defragger. This function can be called from another thread to
stop the defragger or the analyzer. */
DEFRAG_API int DefragInitialize(
PDefragShowMessageCallback ShowMessageFunction,
PDefragShowMoveCallback ShowMoveFunction,
PDefragShowAnalyzeCallback ShowAnalyzeFunction,
PDefragShowDebugCallback ShowDebugFunction,
PDefragDrawClusterCallback DrawClusterFunction,
PDefragClearScreenCallback ClearScreenFunction,
char **DebugMessagesArray) {
ShowMessageCallback = ShowMessageFunction;
ShowMoveCallback = ShowMoveFunction;
ShowAnalyzeCallback = ShowAnalyzeFunction;
ShowDebugCallback = ShowDebugFunction;
DrawClusterCallback = DrawClusterFunction;
ClearScreenCallback = ClearScreenFunction;
DebugMsg = DebugMessagesArray;
return(0);
}
-------------------------------
et en fin comment declarer function "DefragInitialize".
merci inf
a tous ...
)