0

C# side:

    [DllImport(@"FileGuidUtils.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode)]
    private static extern long getReparseType([MarshalAsAttribute(UnmanagedType.LPWStr)] string linkPath);

C/C++ side:

__declspec(dllexport) ReparseType getReparseType(WCHAR *linkPath) {
    HANDLE hFile = CreateFile(linkPath, GENERIC_ALL, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OPEN_REPARSE_POINT, NULL);
    if (hFile == INVALID_HANDLE_VALUE)
    {
        CloseHandle(hFile);
        return NEITHER;
    }

    REPARSE_DATA_BUFFER *reparseDataBuffer = (REPARSE_DATA_BUFFER *)malloc(MAXIMUM_REPARSE_DATA_BUFFER_SIZE);
    DWORD dwRetLen = 0;
    BOOL bRet = DeviceIoControl(hFile, FSCTL_GET_REPARSE_POINT, NULL, 0, reparseDataBuffer, MAXIMUM_REPARSE_DATA_BUFFER_SIZE, &dwRetLen, NULL);
    if (bRet == FALSE)
    {
        free(reparseDataBuffer);
        CloseHandle(hFile);
        return NEITHER;
    }

    ULONG reparseType = reparseDataBuffer->ReparseTag;
    free(reparseDataBuffer);
    CloseHandle(hFile);

    if (reparseType == IO_REPARSE_TAG_SYMLINK) {
        return SYMLINK;
    }
    else if (reparseType == IO_REPARSE_TAG_MOUNT_POINT) {
        return JUNCTION;
    }

    return NEITHER;
}

How is linkPath passed? Is it passed as a malloc'd string and I need to free it up on the C/C++ side or will the CLR take care of it for me? If it is getting cleaned up, am I passing it correctly? Could I just pass it as an ordinary C# string without the [MarshalAsAttribute(UnmanagedType.LPWStr)]? Thanks!

1 Answer 1

1

C# will make a copy of the C# string for you into unmanaged memory , pass a pointer to it and delete the string once the function call is finished

Sign up to request clarification or add additional context in comments.

1 Comment

OK so as I understand what you've said, there's nothing I need to change?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.