CoolVeryVery 1 settimana fa
parent
commit
be144285f7
4 ha cambiato i file con 91 aggiunte e 31 eliminazioni
  1. 72 22
      DLLWin/CodeDLL.c
  2. 1 1
      UsingDLLWinApi/UsingDLLWinApi.vcxproj
  3. 17 7
      UsingDLLWinApi/main.cpp
  4. 1 1
      structs.h

+ 72 - 22
DLLWin/CodeDLL.c

@@ -1,6 +1,8 @@
+#define _CRT_SECURE_NO_WARNINGS
 #include <windows.h>
 #include <stdio.h>
 #include <malloc.h>
+#include <string.h>
 #include "../structs.h"
 
 BOOL WINAPI DllMain(HINSTANCE hlnstDll, DWORD dwReason, LPVOID IpReserved)
@@ -31,31 +33,79 @@ BOOL WINAPI DllMain(HINSTANCE hlnstDll, DWORD dwReason, LPVOID IpReserved)
 //	MessageBox(NULL, str, L"ß äà", MB_OK);
 //	return 0;
 //}
-extern "C" __declspec(dllimport) ResultUserAndCount* GetFirstAndCounter(HANDLE file_read);
-ResultUserAndCount* GetFirstAndCounter(HANDLE file_read) {
-	ResultUserAndCount* result = (ResultUserAndCount*)malloc(sizeof(ResultUserAndCount));
+int Convert_sub(char* sub) {
+	int result = 0;
+	int ymnoz = 1;
+	for (size_t i = 0; i < strlen(sub); i++)
+	{
+		result *= ymnoz;
+		result += sub[i] - '0';
+		ymnoz *= 10;
+	}
+	return result;
+}
 
+extern "C" __declspec(dllimport) ResultUserAndCount GetFirstAndCounter(FILE* file_read);
+ResultUserAndCount GetFirstAndCounter(FILE* file_read) {
+	ResultUserAndCount result = { NULL, 0 };
+	User* list_user = (User*)malloc(sizeof(User) * 200);
 	char* buffer = (char*)malloc(sizeof(char) * 100);
-	DWORD count_buff = 100;
-	DWORD count_char = 0;
-	while (ReadFile(
-		file_read,
-		buffer + (count_buff - 100),
-		count_buff,
-		&count_char,
-		NULL
-	) && count_char > 0) {
-		count_buff += 100;
-		buffer = (char*)realloc(buffer, sizeof(char) * count_buff);
+	int index_start = 0;
+	int index_list = 0;
+	int count_user = 0;
+	int count_sub_char = 0;
+	while (fscanf(file_read, "%s", buffer) == 1) {
+		for (size_t i = 0; i < strlen(buffer); i++)
+		{
+			if (buffer[i] != '\r' && buffer[i] != '\n' && buffer[i] != '\"') {
+				if (buffer[i] == ';')
+				{	
+					char* sub = (char*)malloc(sizeof(char) * 100);
+					int index_sub = 0;
+					for (size_t j = index_start + 1; j < i; j++)
+					{
+						sub[index_sub] = buffer[j];
+						index_sub++;
+					}			
+					sub[index_sub] = '\0';
+					index_start = i;
+					switch (count_sub_char)
+					{
+					case 0:
+						list_user[index_list].last_name = sub;
+						break;
+					case 1:
+						list_user[index_list].first_name = sub;
+						break;
+					case 2:
+						list_user[index_list].patrinymic = sub;
+						break;
+					default:
+						break;
+					}
+					count_sub_char++;
+					
+				}
+			}
+			else if (buffer[i] == '\"' && count_sub_char != 0) {
+				char* sub = (char*)malloc(sizeof(char) * 100);
+				int index_sub = 0;
+				for (size_t j = index_start + 1; j < i; j++)
+				{
+					sub[index_sub] = buffer[j];
+					index_sub++;
+				}
+				sub[index_sub] = '\0';
+				list_user[index_list].age = Convert_sub(sub);
+				count_sub_char = 0;
+				index_start = 0;
+				index_list++;
+				count_user++;
+			}
+		}
 
 	}
-	CloseHandle(file_read);
-	printf("%s", buffer);
-	result->count_users = 1;
-	result->user = (User*)malloc(sizeof(User));
-	result->user->last_name = _strdup("Ïðèìåð");
-	result->user->first_name = _strdup("Ôàìèëèÿ");
-	result->user->patrinymic = _strdup("Îò÷åñòâî");
-	result->user->age = 0; //Èëè êàêîå-òî çíà÷åíèå ïî óìîë÷àíèþ
+	result.count_users = count_user;
+	result.user = list_user[0];
 	return result;
 }

+ 1 - 1
UsingDLLWinApi/UsingDLLWinApi.vcxproj

@@ -107,7 +107,7 @@
       <CompileAs>CompileAsCpp</CompileAs>
     </ClCompile>
     <Link>
-      <SubSystem>Windows</SubSystem>
+      <SubSystem>Console</SubSystem>
       <GenerateDebugInformation>true</GenerateDebugInformation>
     </Link>
   </ItemDefinitionGroup>

+ 17 - 7
UsingDLLWinApi/main.cpp

@@ -1,13 +1,14 @@
+#define _CRT_SECURE_NO_WARNINGS
 #include <windows.h>
 #include <stdio.h>
 #define PATH L"DLLWin.dll"
 #include "../structs.h"
 
 
-typedef ResultUserAndCount*(_cdecl* MyFunction)(HANDLE);
+typedef ResultUserAndCount(_cdecl* MyFunction)(FILE*);
 
 
-int WINAPI WinMain(HINSTANCE l, HINSTANCE s, LPSTR d, int f) {
+int main() {
 
 	HINSTANCE hMyDll;
 	if ((hMyDll = LoadLibrary(PATH)) == NULL) return 1;
@@ -15,8 +16,15 @@ int WINAPI WinMain(HINSTANCE l, HINSTANCE s, LPSTR d, int f) {
 	
 
 
-	HANDLE FileHandle;
-	FileHandle = CreateFileW(
+	FILE* FileHandle = fopen("ds.csv", "r");
+	/*wchar_t* buffer = (wchar_t*)malloc(sizeof(wchar_t) * 100);
+	DWORD count_buff = 100;
+	DWORD count_char = 0;
+	while (fscanf(FileHandle, "%s", buffer) == 1) {
+		MessageBox(NULL, buffer, L"Îøèáêà", MB_ICONERROR);
+		printf("%s", buffer);
+	}*/
+	/*	CreateFileW(
 		L"ds.csv",
 		GENERIC_READ,
 		FILE_SHARE_READ,
@@ -24,12 +32,14 @@ int WINAPI WinMain(HINSTANCE l, HINSTANCE s, LPSTR d, int f) {
 		OPEN_EXISTING,
 		FILE_ATTRIBUTE_NORMAL,
 		0
-	);
+	);*/
 	if (FileHandle == INVALID_HANDLE_VALUE) {
 		int error = GetLastError();
-		MessageBox(NULL, L"Îøèáêà îòêðûòèÿ ôàéëà!", L"Îøèáêà", MB_ICONERROR);
 	}
-	myFun(FileHandle);
+	ResultUserAndCount result = myFun(FileHandle);
+	char* buf = (char*)malloc(sizeof(char) * 100);
+	snprintf(buf, sizeof buf, "%d", result.count_users);
+	printf("%s", buf);
 	FreeLibrary(hMyDll);
 	return 0;
 }

+ 1 - 1
structs.h

@@ -9,7 +9,7 @@ struct User {
 };
 
 struct ResultUserAndCount {
-	struct User* user;
+	struct User user;
 	int count_users;
 };
 #endif // !STRUCTS_H