123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390 |
- DLL
- #include "../DllдляПрикладного/Header.h"//подключил заголовок из другого проекта
- BOOL WINAPI DllMain(HINSTANCE hlnstDll, DWORD dwReason, LPVOID IpReserved)
- {
- BOOL bAllWentWell = TRUE;
- switch (dwReason)
- {
- case DLL_PROCESS_ATTACH:
- break;
- case DLL_THREAD_ATTACH:
- break;
- case DLL_THREAD_DETACH:
- break;
- case DLL_PROCESS_DETACH:
- break;
- }
- if (bAllWentWell)
- return TRUE;
- else
- return FALSE;
- }
- LPWSTR Reading(LPWSTR path)
- {
- DWORD d = 0;
- HINSTANCE FileHandle = CreateFile(PATH, GENERIC_READ, FILE_SHARE_WRITE | FILE_SHARE_READ, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
- LPWSTR ReadString = calloc(100500, sizeof(WCHAR));
- ReadFile(FileHandle, ReadString, 100500 * sizeof(WCHAR), &d, NULL);
- CloseHandle(FileHandle);
- ReadString[d / 2] = '\n';
- ReadString[d / 2 + 1] = '\0';
- return ReadString;
- }
- DWORD LpwstrToDword(LPWSTR str)
- {
- DWORD dw = 0;
- for (size_t i = 0; i < wcslen(str); i++)
- {
- dw += (str[i] - '0');
- dw *= 10;
- }
- return dw / 10;
- }
- DWORD CountUsers(LPWSTR str)
- {
- DWORD count = 0;
- for (size_t i = 0; i < wcslen(str); i++)
- {
- if (str[i] == L'\n') count++;
- }
- return count;
- }
- __declspec(dllexport) U* CreateUserArray(LPWSTR path, LPDWORD n)
- {
- LPWSTR str = Reading(path);
- DWORD count = CountUsers(str);
- U* users = calloc(count, sizeof(U));
- DWORD poz = 0, zap = 0, ind = 0;
- LPWSTR strvozr = calloc(3, sizeof(TCHAR));
- for (size_t i = 1; i < wcslen(str); i++)
- {
- if (str[i] == '\n')
- {
- strvozr = calloc(3, sizeof(TCHAR));
- zap++;
- poz = 0;
- ind = 0;
- }
- else
- {
- if (str[i] == ';')
- {
- poz++;
- ind = 0;
- }
- else
- {
- if (poz == 0)
- users[zap].f[ind] = str[i];
- if (poz == 1)
- users[zap].i[ind] = str[i];
- if (poz == 2)
- users[zap].o[ind] = str[i];
- if (poz == 3)
- users[zap].g[ind] = str[i];
- if (poz == 4)
- {
- if (str[i] == '\r')
- {
- strvozr[ind] = '\0';
- users[zap].vozr = LpwstrToDword(strvozr);
- free(strvozr);
- }
- else strvozr[ind] = str[i];
- }
- ind++;
- }
- }
- }
- *n = count;
- return users;
- }
- __declspec(dllexport) U1* Ubel(U* users, LPDWORD count)
- {
- DWORD indUser = 0, con = 0, f = 0;
- for (DWORD i = 0; i < *count; i++)
- {
- if (users[i].vozr%5==0)
- {
- con++;
- }
- }
- con++;
- U1* Ubel = calloc(con, sizeof(U1));
- Ubel[0].f[0] = (WCHAR)65279;
- indUser++;
- for (DWORD i = 0; i < *count; i++)
- {
- if (users[i].vozr % 5 == 0)
- {
- for (DWORD j = 0; j < wcslen(users[i].o); j++)
- {
- Ubel[indUser].f[j] = users[i].f[j];
- }
- for (DWORD j = 0; j < wcslen(users[i].i); j++)
- {
- Ubel[indUser].i[j] = users[i].i[j];
- }
- for (DWORD j = 0; j < wcslen(users[i].o); j++)
- {
- Ubel[indUser].o[j] = users[i].o[j];
- }
- Ubel[indUser].vozr = users[i].vozr;
- indUser++;
- }
- }
- *count = con;
- return Ubel;
- }
- __declspec(dllexport) float Age(U* users, LPDWORD count)
- {
- float avg = 0;
- for (DWORD i = 1; i < *count; i++)
- {
- avg += users[i].vozr;
- }
- avg = avg / (*count - 1);
- return avg;
- }
- __declspec(dllexport) LPWSTR Dlina(U* users, LPDWORD count)
- {
- DWORD ind = 0, indUser = 0, f = 0;
- DWORD max = 0;
- for (DWORD i = 0; i < *count; i++)
- {
- size_t l1 = strlen(users[i].f);
- size_t l2 = strlen(users[i].i);
- size_t l3 = strlen(users[i].o);
- if (l1 + l2 + l3 > max)
- {
- max = l1 + l2 + l3;
- ind = i;
- }
- }
- char third[512];
- swprintf(third, sizeof(third), L"%s%s%s", users[ind].f, users[ind].i, users[ind].o);
- return third;
- }
- __declspec(dllexport) DWORD kolvo(U* users, LPDWORD count)
- {
- DWORD ind = 0, indUser = 0, con = 0, f = 0;
- WCHAR str = L'м';
-
- for (DWORD i = 0; i < *count; i++)
- {
- if (users[i].g[0] == str) con++;
- }
-
- *count = con;
- return con;
- }
- }
- Main
- #include "header.h"
- #include "header1.h"
- #include <strsafe.h>
- #define PATHDLL L"DLL.dll"
- #define PATHLIST L"Users.csv"
- typedef U1* (_cdecl* MyFunction)(LPWSTR, LPDWORD);
- typedef U* (_cdecl* MyFunction1)(U*, LPDWORD);
- typedef float(_cdecl* MyFunction2) (U*, LPDWORD);
- typedef LPWSTR(_cdecl* MyFunction3) (U*, LPDWORD);
- typedef DWORD(_cdecl* MyFunction4) (U*, LPDWORD);
- int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
- {
- //Задание 1
- HINSTANCE MyDLL;
- if (!(MyDLL = LoadLibrary(PATHDLL))) return 1;
- MyFunction UserList = (MyFunction)GetProcAddress(MyDLL, "CreateUserArray");
- MyFunction1 UserList1 = (MyFunction1)GetProcAddress(MyDLL, "Ubel");
- MyFunction2 AvgUserList = (MyFunction2)GetProcAddress(MyDLL, "Age");
- MyFunction3 MaxFIOUser = (MyFunction3)GetProcAddress(MyDLL, "Dlina");
- MyFunction4 KolvoM = (MyFunction4)GetProcAddress(MyDLL, "kolvo");
- DWORD count = 0;
- U* users = UserList(PATH, &count);
- DWORD w;
- HINSTANCE FileHandle = CreateFile(L"1.csv", GENERIC_WRITE, FILE_SHARE_WRITE | FILE_SHARE_READ, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
- TCHAR otvFile[512];
- TCHAR BuferObmena[512];
- for (size_t i = 0; i < count-1; i++)
- {
- users[i].f[wcslen(users[i].f)] = (WCHAR)';';
- WriteFile(FileHandle, users[i].f, wcslen(users[i].f) * sizeof(WCHAR), &w, NULL);
- users[i].i[wcslen(users[i].i)] = (WCHAR)';';
- WriteFile(FileHandle, users[i].i, wcslen(users[i].i) * sizeof(WCHAR), &w, NULL);
- users[i].o[wcslen(users[i].o)] = (WCHAR)';';
- WriteFile(FileHandle, users[i].o, wcslen(users[i].o) * sizeof(WCHAR), &w, NULL);
- users[i].g[wcslen(users[i].g)] = (WCHAR)';';
- WriteFile(FileHandle, users[i].g, wcslen(users[i].g) * sizeof(WCHAR), &w, NULL);
- swprintf(otvFile, 512, TEXT("%d\r\n"), users[i].vozr);
- // swprintf(BuferObmena, 512, TEXT("%s;%s;%s;$s\0"), users[i].vozr);
-
- DWORD len = 0;
- while (otvFile[len] != '\0')
- {
- len++;
- }
- DWORD dz = 0;
- WriteFile(FileHandle,
- otvFile,
- len * (sizeof(char) * 2),
- &dz,
- NULL
- );
- }
- CloseHandle(FileHandle);
- //Задание 2
- DWORD w1;
- TCHAR otvFile2[512];
- DWORD count2 = count;
- U1* ubel = UserList1(users, &count2);
- HINSTANCE FileHandle2 = CreateFile(L"2.txt", GENERIC_WRITE, FILE_SHARE_WRITE | FILE_SHARE_READ, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
- for (size_t i = 1; i < count2-1; i++)
- {
- ubel[i].f[wcslen(ubel[i].f)] = (WCHAR)' ';
- WriteFile(FileHandle2, ubel[i].f, wcslen(ubel[i].f) * sizeof(WCHAR), &w1, NULL);
- ubel[i].i[wcslen(ubel[i].i)] = (WCHAR)' ';
- WriteFile(FileHandle2, ubel[i].i, wcslen(ubel[i].i) * sizeof(WCHAR), &w1, NULL);
- ubel[i].o[wcslen(ubel[i].o)] = (WCHAR)' ';
- WriteFile(FileHandle2, ubel[i].o, wcslen(ubel[i].o) * sizeof(WCHAR), &w1, NULL);
- swprintf(otvFile2, 512, TEXT("%d\r\n"), ubel[i].vozr);
- DWORD len = 0;
- while (otvFile2[len] != '\0')
- {
- len++;
- }
- DWORD dz = 0;
- WriteFile(FileHandle2,
- otvFile2,
- len * (sizeof(char) * 2),
- &dz,
- NULL
- );
- }
- CloseHandle(FileHandle2);
- //Задание 3 Средний возраст
- HKEY hKey = NULL;
- if (RegOpenKey(HKEY_CURRENT_USER, NULL, &hKey) != ERROR_SUCCESS)
- return 1;
- DWORD avg = AvgUserList(users, &count);
- /*avg = (avg + AvgUserList(users, &count)) / 2;*/
- if (RegSetValueEx(hKey, TEXT("ParamSize"), 0, REG_DWORD, (const BYTE*)&avg, sizeof(avg)) == ERROR_SUCCESS)
- {
- MessageBox(NULL, L"Ключ успешно создан", L"MyKey", MB_OK | MB_ICONINFORMATION);
- }
- DWORD DataType = 0;
- DWORD DataLen = 100500;
- DWORD SIZEE;
- TCHAR sZ[100];
- if (RegGetValueW(HKEY_CURRENT_USER, NULL, L"ParamSIze", RRF_RT_DWORD, &DataType, &SIZEE, &DataLen) == ERROR_SUCCESS)
- {
- StringCbPrintf(sZ, 100, TEXT("%d"), SIZEE);
- MessageBox(NULL, sZ, L"Средний возраст", MB_OK | MB_ICONINFORMATION);
- }
- else
- {
- MessageBox(NULL, L"Что-то пошло не так", L"Информация", MB_OK);
- }
- RegCloseKey(hKey);
- //Задание 3 Сама длинная ФИО
- HKEY hKey2 = NULL;
- if (RegOpenKey(HKEY_CURRENT_USER, NULL, &hKey2) != ERROR_SUCCESS)
- return 1;
- if (RegSetValueW(hKey2, L"Mykey", REG_SZ, L"Значение по умолчанию", 22 * sizeof(WCHAR)) == ERROR_SUCCESS)
- {
- MessageBox(NULL, L"Ключ успешно создан", L"Информация", MB_OK);
- }
- LPWSTR maxst = MaxFIOUser(users, &count);
- TCHAR buff2[512];
- wsprintf(buff2, L"%s", maxst);
- if (RegSetValueExW(hKey2, L"Mykeys", NULL, REG_SZ, &buff2, 22 * sizeof(WCHAR)) == ERROR_SUCCESS)
- {
- MessageBox(NULL, &buff2, L"ФИО", MB_OK | MB_ICONINFORMATION);
- }
- else
- {
- MessageBox(NULL, L"Что-то пошло не так", L"Информация", MB_OK);
- }
- RegCloseKey(hKey2);
- //Задание 3 Количество мужчин
- HKEY hKey3 = NULL;
- if (RegOpenKey(HKEY_CURRENT_USER, NULL, &hKey3) != ERROR_SUCCESS)
- return 1;
- DWORD kol = KolvoM(users, &count);
- if (RegSetValueEx(hKey3, TEXT("ParamSize3"), 0, REG_DWORD, (const BYTE*)&kol, sizeof(kol)) == ERROR_SUCCESS)
- {
- MessageBox(NULL, L"Ключ успешно создан", L"MyKey3", MB_OK | MB_ICONINFORMATION);
- }
- DWORD DataType2 = 0;
- DWORD DataLen2 = 100500;
- DWORD SIZEE2;
- TCHAR sZ2[100];
- if (RegGetValueW(HKEY_CURRENT_USER, NULL, L"ParamSIze3", RRF_RT_DWORD, &DataType2, &SIZEE2, &DataLen2) == ERROR_SUCCESS)
- {
- StringCbPrintf(sZ2, 100, TEXT("%d"), SIZEE2);
- MessageBox(NULL, sZ2, L"Количесво мужчин", MB_OK | MB_ICONINFORMATION);
- }
- else
- {
- MessageBox(NULL, L"Что-то пошло не так", L"Информация", MB_OK);
- }
- ClipboardInputText(otvFile);
- RegCloseKey(hKey3);
- }
- Заголовков 1
- #pragma once
- #define _CRT_SECURE_NO_WARNINGS
- #include <Windows.h>
- #include <stdio.h>
- #define PATH L"Users.csv"
- struct User
- {
- WCHAR f[30];
- WCHAR i[30];
- WCHAR o[30];
- WCHAR g[5];
- DWORD vozr;
- };
- typedef struct User U;
- struct Ubel
- {
- WCHAR f[30];
- WCHAR i[30];
- WCHAR o[30];
- WCHAR g[5];
- DWORD vozr;
- };
- typedef struct Ubel U1;
- Заголовков 2
- #pragma once
- #define _CRT_SECURE_NO_WARNINGS
- #include <Windows.h>
- #include <stdio.h>
- int ClipboardInputText(LPWSTR buffer)//записать строку в системный буфер
- {
- DWORD len;
- HANDLE hMenu;
- len = wcslen(buffer) + 1;
- hMenu = GlobalAlloc(GMEM_MOVEABLE, len * sizeof(LPWSTR));
- memcpy(GlobalLock(hMenu), buffer, len * sizeof(LPWSTR));
- GlobalUnlock(hMenu);
- OpenClipboard(0);
- EmptyClipboard();
- SetClipboardData(CF_UNICODETEXT, hMenu);
- CloseClipboard();
- return 0;
- }
|