Twitter iPhone pliant OnePlus 11 PS5 Disney+ Orange Livebox Windows 11

Savez-vous transposer un programme en C et Windev ?

1 réponse
Avatar
GP
Bonjour, tout est dans la question ?

Le but est de transposer un petit programme en C en une procédure
Windev

Le but est de simplifier ce programme pour permettre de commander
l'éjection de la disquette correspondant au support D:\ ou E\: etc..
qui lui sera passé en paramètre

Le programme qui est à adapter récupère la liste des supports
susceptibles d'être ejectés et les éjecte.

Si vous êtes partant je vous envoie le fichier ZIP contenant le code et
l'explication de sa procédure
Ce programme est adapté aux divers Windows : conserver ces possibilités
précieusement
Pour avoir une idée du programme voici le code :

//***************************** Eject **************************
// Programme d'éjection de disque amovible à la sortie de Windows

#include <windows.h>
#include <winioctl.h>

// Définitions
#define VWIN32_DIOC_DOS_IOCTL 1

#define OPTION_VERIFY 0x01
#define OPTION_NOW 0x02
#define OPTION_SUSPEND 0x04
#define OPTION_NOT_ENDSESSION 0x08
#define OPTION_ALL 0x10
#define OPTION_LOAD 0x20


// Variables globales
char g_szLecteurs[100]; // Liste des lecteurs à éjecter
DWORD g_dwOptions; // Liste des options
WORD g_wWaitTime; // Durée de la pause après éjection

// Structures
typedef struct _DIOC_REGISTERS {
DWORD reg_EBX;
DWORD reg_EDX;
DWORD reg_ECX;
DWORD reg_EAX;
DWORD reg_EDI;
DWORD reg_ESI;
DWORD reg_Flags;
} DIOC_REGISTERS, *PDIOC_REGISTERS;

// Prototypes
void Eject(int nDrive);
void Load(int nDrive);
long WINAPI WndProc(HWND hwnd, UINT message, WPARAM wParam,LPARAM
lParam);
BOOL VerifyMedia(int nMedia);
void EjectProcess(void);

// Boucle principale du programme
int PASCAL WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpszCmdLine, int nCmdShow)
{
MSG msg;
WNDCLASS wndclass;
HWND hWindow;
char szChaine[100];
char* szBloc;

if(hPrevInstance)
return NULL;

if(!lpszCmdLine[0])
return NULL;

g_dwOptions = NULL;
g_wWaitTime = NULL;
lstrcpy(szChaine,lpszCmdLine);
_strupr(szChaine);

// Examen de la ligne de commande
szBloc = strtok(szChaine, " ");
if(szBloc[0]=='-') // Il y a des options
{
for(int i=1; i<lstrlen(szBloc); i++)
{
switch(szBloc[i])
{
case 'V':
g_dwOptions |= OPTION_VERIFY;
break;
case 'N':
g_dwOptions |= OPTION_NOW;
break;
case 'S':
g_dwOptions |= OPTION_SUSPEND;
break;
case 'E':
g_dwOptions |= OPTION_NOT_ENDSESSION;
break;
case 'A':
g_dwOptions |= OPTION_ALL;
lstrcpy(g_szLecteurs, "ABCDEFGHIJKLMNOPQRSTUVWXYZ");
break;
case 'L':
g_dwOptions |= OPTION_LOAD;
break;
case 'W':
{
char szTime[6];
int k=0;
while((i+1)<lstrlen(szBloc) && k<(sizeof(szTime)-1)
&& szBloc[i+1]>='0' && szBloc[i+1]<='9')
szTime[k++]=szBloc[++i];
szTime[k]=0;
g_wWaitTime = atoi(szTime);
}
break;
}
}
szBloc = strtok(NULL, " ");
}

// Si option "Load", il faut forcément un temps d'attente. Si Nul ->
5s par défaut
if((g_dwOptions & OPTION_LOAD) && !g_wWaitTime)
g_wWaitTime = 5;

if(szBloc) // Examens des lecteurs
{
if(g_dwOptions & OPTION_ALL) // On enlève les lecteurs de la liste en
mettant des "_"
{
for(int i=0; i<lstrlen(szBloc); i++)
for(int j=0; j<lstrlen(g_szLecteurs); j++)
if(g_szLecteurs[j]==szBloc[i])
g_szLecteurs[j] = '_';
}
else // La liste des lecteurs est szBloc
lstrcpy(g_szLecteurs, szBloc);
}

// On évite les erreurs d'accès aux lecteurs
SetErrorMode(SEM_FAILCRITICALERRORS);

// Now -> on applique directement
if(g_dwOptions & OPTION_NOW)
{
EjectProcess();
return NULL;
}

wndclass.style = NULL;
wndclass.lpfnWndProc = WndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = NULL;
wndclass.hCursor = NULL;
wndclass.hbrBackground = NULL;
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = "MicromegaEjectWindow";
RegisterClass(&wndclass);

hWindow=CreateWindow("MicromegaEjectWindow", "", WS_DISABLED,
0, 0, 0, 0, NULL,NULL,hInstance,NULL);

while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}

// Procédure d'éjection d'un disque. A=1; B=2; C=3...
void Eject(int nDrive)
{
if(nDrive<1 || nDrive>26)
return;

// Test Version Windows (procédés différents sou NT et 95/98)
OSVERSIONINFO OSVer;
OSVer.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
GetVersionEx(&OSVer);

if(OSVer.dwPlatformId==VER_PLATFORM_WIN32_WINDOWS)
{
// Ejection sous Windows 95/98/Me
HANDLE hDevice;
DIOC_REGISTERS reg;
BOOL fResult;
DWORD cb;

hDevice = CreateFile("\\\\.\\vwin32",
0, 0, NULL, 0, FILE_FLAG_DELETE_ON_CLOSE, NULL);

reg.reg_EAX = 0x440D; // IOCTL for block devices
reg.reg_EBX = nDrive; // zero-based drive identifier
reg.reg_ECX = 0x0849; // Eject Media
reg.reg_Flags = 0x0001; // assume error (carry flag is set)

fResult = DeviceIoControl(hDevice,
VWIN32_DIOC_DOS_IOCTL,
&reg, sizeof(reg),
&reg, sizeof(reg),
&cb, 0);

CloseHandle(hDevice);
}
else
{
// Ejection sous Windows NT/2000/XP
HANDLE hDevice;
char szVolume[8];
DWORD dwBytesReturned;

wsprintf(szVolume, "\\\\.\\%c:", 'A'+nDrive-1);
hDevice = CreateFile(szVolume, GENERIC_READ, FILE_SHARE_READ |
FILE_SHARE_WRITE,
NULL, OPEN_EXISTING, 0, NULL);

DeviceIoControl(hDevice, IOCTL_STORAGE_EJECT_MEDIA, NULL, 0, NULL, 0,
&dwBytesReturned, NULL);

CloseHandle(hDevice);
}
}

// Vérifie si un media est présent de le lecteur indiqué
BOOL VerifyMedia(int nMedia)
{
char szPath[10] = "X:\\";

// On place la bonne lettre en fct de nMedia: 0=A, 2=B...
szPath[0] = nMedia+'A';

return GetVolumeInformation(szPath, NULL, NULL, NULL, NULL, NULL,
NULL, NULL);
}

// Procédure pour refermer les disques
void Load(int nDrive)
{
if(nDrive<1 || nDrive>26)
return;

// Test Version Windows (procédés différents sou NT et 95/98)
OSVERSIONINFO OSVer;
OSVer.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
GetVersionEx(&OSVer);

if(OSVer.dwPlatformId==VER_PLATFORM_WIN32_WINDOWS)
{
// Fermeture sous Windows 95/98/Me: non supporté
/* HANDLE hDevice;
DIOC_REGISTERS reg;
BOOL fResult;
DWORD cb;

hDevice = CreateFile("\\\\.\\vwin32",
0, 0, NULL, 0, FILE_FLAG_DELETE_ON_CLOSE, NULL);

reg.reg_EAX = 0x440D; // IOCTL for block devices
reg.reg_EBX = nDrive; // zero-based drive identifier
reg.reg_ECX = 0x0849; // Eject Media
reg.reg_Flags = 0x0001; // assume error (carry flag is set)

fResult = DeviceIoControl(hDevice,
VWIN32_DIOC_DOS_IOCTL,
&reg, sizeof(reg),
&reg, sizeof(reg),
&cb, 0);

CloseHandle(hDevice);*/
}
else
{
// Fermeture sous Windows NT/2000/XP
HANDLE hDevice;
char szVolume[8];
DWORD dwBytesReturned;

wsprintf(szVolume, "\\\\.\\%c:", 'A'+nDrive-1);
hDevice = CreateFile(szVolume, GENERIC_READ, FILE_SHARE_READ |
FILE_SHARE_WRITE,
NULL, OPEN_EXISTING, 0, NULL);

DeviceIoControl(hDevice, IOCTL_STORAGE_LOAD_MEDIA, NULL, 0, NULL, 0,
&dwBytesReturned, NULL);

CloseHandle(hDevice);
}
}

// Processus d'éjection des lecteurs désignés
void EjectProcess(void)
{
for(int i=0; g_szLecteurs[i]; i++)
if(g_szLecteurs[i]>='A' && g_szLecteurs[i]<='Z')
if((g_dwOptions & OPTION_VERIFY) ? VerifyMedia(g_szLecteurs[i]-'A') :
TRUE)
Eject(g_szLecteurs[i]-'A'+1);

// Pause après éjection
if(g_wWaitTime)
Sleep((DWORD)g_wWaitTime*1000L);

// Si option "L", fermeture des lecteurs
if(g_dwOptions & OPTION_LOAD)
{
for(int i=0; g_szLecteurs[i]; i++)
if(g_szLecteurs[i]>='A' && g_szLecteurs[i]<='Z')
Load(g_szLecteurs[i]-'A'+1);
}
}

// Fenêtre destinée à recevoir les messages
long WINAPI WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM
lParam)
{
switch(message)
{
case WM_ENDSESSION:
if(g_dwOptions & OPTION_NOT_ENDSESSION) // Pas de traitement!!
return 0;
if((BOOL)wParam && !(BOOL)lParam)
{
EjectProcess();
}
return 0;

case WM_POWERBROADCAST:
if((wParam==PBT_APMSUSPEND) && (g_dwOptions & OPTION_SUSPEND))
{
EjectProcess();
}
return DefWindowProc(hwnd,message,wParam,lParam);

case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd,message,wParam,lParam);
}

--
Ceci est une signature automatique de MesNews.
Site : http://mesnews.no-ip.com

1 réponse

Avatar
T. Pruvot
Utilisez WDAPI dispo sur le site de pcsoft il me semble. pour la conversion
des structures et constantes C