Created
January 16, 2025 15:56
-
-
Save Naetmul/81b38fb08f12878c39742b2019cb5e90 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import ctypes | |
| # Load the DLL | |
| dll = ctypes.CDLL("path_to_your_dll.dll") | |
| # Set the argument and return types for the function | |
| dll.ReadDocx.argtypes = [ctypes.c_char_p] | |
| dll.ReadDocx.restype = ctypes.c_char_p | |
| # Call the function | |
| docx_path = b"C:\\path_to_document\\example.docx" | |
| output_text = dll.ReadDocx(docx_path) | |
| # Print the extracted text | |
| print(output_text.decode('utf-8')) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <windows.h> | |
| #include <comdef.h> | |
| #include <string> | |
| // Import the Microsoft Word Object Library | |
| #import "C:\\Program Files\\Microsoft Office\\Root\\Office16\\MSWORD.OLB" rename("ExitWindows", "ExitWindowsEx") | |
| using namespace Word; | |
| // Exported function signature | |
| extern "C" __declspec(dllexport) const char* ReadDocx(const char* filepath) { | |
| static std::string result; // Static to avoid returning dangling pointers | |
| CoInitialize(NULL); // Initialize COM | |
| try { | |
| // Create Word application object | |
| ApplicationPtr pWordApp(__uuidof(Application)); | |
| pWordApp->Visible = false; // Run Word in invisible mode | |
| // Open the document with wdOpenFormatUnicodeText | |
| DocumentsPtr pDocs = pWordApp->Documents; | |
| _bstr_t bstrFile(filepath); | |
| _DocumentPtr pDoc = pDocs->Open( | |
| bstrFile, // FileName | |
| _variant_t(false), // ConfirmConversions | |
| _variant_t(true), // ReadOnly | |
| _variant_t(false), // AddToRecentFiles | |
| _variant_t(), // PasswordDocument | |
| _variant_t(), // PasswordTemplate | |
| _variant_t(), // Revert | |
| _variant_t(), // WritePasswordDocument | |
| _variant_t(), // WritePasswordTemplate | |
| _variant_t(5), // Format = wdOpenFormatUnicodeText | |
| _variant_t() // Encoding | |
| ); | |
| // Get the body text | |
| _bstr_t bstrText = pDoc->Content->Text; | |
| result = (const char*)bstrText; | |
| // Close the document and Word application | |
| pDoc->Close(false); | |
| pWordApp->Quit(); | |
| } catch (_com_error& e) { | |
| // Handle COM errors | |
| result = "Error: "; | |
| result += (const char*)e.Description(); | |
| } | |
| CoUninitialize(); // Uninitialize COM | |
| return result.c_str(); | |
| } | |
| 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; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment