Creating basic C ++. Dll for p / invoke in C #

I am a C # programmer, and unfortunately, due to my age and experience, I didn’t have the luxury of getting the chance to go through the era of C ++ programming in my training - so much is mysterious and new to me, Not really here, to discuss the importance of learning this or not, but I need some help on what should be a trivial matter.

PROBLEM

I need help packing my C ++ code in a DLL. I have no experience with C ++, and I am having difficulty creating a working .dll with which I can p / invoke (Visual Studio 2010). Please continue reading for more information and the code I'm trying to package.

DETAILS

I have code to run in an unmanaged environment. Under normal conditions, a simple p / invoke is suitable for this task. Connect a few [ImportDll]and you're good to go. In the worst case, I can sometimes use Marshalling. However, for reasons that I have not yet discovered, this is not possible with my current task.

I am trying to read some of the data owner drawn list boxthat was made in an old unmanaged C ++ application and get text from it. I have seen some examples of how to do this, but they are in the old VB6 code.

I found the C ++ equivalent, but this is also not suitable for me. Even with p / invoke, there seem to be a lot of problems with moving the memory and how it should work. The process is quite simplified.

++.dll, , , .

, , , . ++ Wizards Visual Studio ( Visual Studio 2010), . , - , , , , .

, .dll.

hListWnd, IntPtr #, index Int32, outputResult , . p/invoke System.Text.StringBuilder, -, .

void GetListItemData( HWND hListWnd, long index, char *outputResult )
{
    int result;
    DWORD processID;
    HANDLE hProcess;
    char *itemData;
    char sDataRead[5];
    DWORD bytes;
    DWORD lListItemHold, lListItemDataHold;
    *outputResult=0;

    if( hListWnd )
    {
        GetWindowThreadProcessId( hListWnd, &processID );

        hProcess=OpenProcess( 0x10|0xf0000|PROCESS_VM_READ, 0, processID );

        if( hProcess )
        {
            lListItemHold=(DWORD)SendMessage( hListWnd, LB_GETITEMDATA, index-1, 0 );
            lListItemHold=lListItemHold+24;

            result=ReadProcessMemory( hProcess, (void *)lListItemHold, &sDataRead, 4, &bytes );
            if( !result )
            {
                RaiseWinErr();
            }

            memcpy( &lListItemDataHold, &sDataRead, 4 );
            lListItemDataHold=lListItemDataHold+6;

            ReadProcessMemory( hProcess, (void *)lListItemDataHold, outputResult, 16, &bytes );

            CloseHandle( hProcess );
        }
    }
}

- , , . . # RtlMoveMemory ReadProcessMemory p/invoke, . . , ( , , +6 +24, ..), , , t . , , , , , , List Item .

, , IAccessible Accessibility.dll. . ManagedWinApi pinvoke.net, .

, . #. , [DllImport] # .

, - .

+3
1

[], char *, 16.

ReadProcessMemory, , 16 . , 16- . - [] 16. , ++ char # char - # char ++ wchar_t Windows. ++ char - #.

, - , , ReadProcessMemory, .

.dll( , .dll), , ++ ,

extern "C" __declspec(dllexport) void GetListItemData( HWND hListWnd, long index, char *outputResult )

++, DLL ( ). - , , , .

#

[System.Runtime.InteropServices.DllImport(
    DLLPath,
    CallingConvention = CallingConvention.Cdecl
)]
private static extern void GetListItemData(
    System.IntPtr hWnd,
    System.Int32 index,
    [MarshalAs(UnmanagedType.LPArray)]byte[] buffer
);

, , .

+5

Source: https://habr.com/ru/post/1785185/


All Articles