简体   繁体   中英

Wrong behaviour when passing string from c# to c dll

I researched a lot about my problem and found many questions relative to [ how to pass string from c# to c dll ] :

But none of them couldn't help me. Any way I prefer to ask my question :


Briefly explanation :
My c function working properly in c# just when I interchange the GetProcessIntegrityLevel parameters (both in c dll and also in c# import-dll) from :

  1. BOOL GetProcessIntegrityLevel(DWORD dwPID, LPSTR szIntegrityLevel);

To this one :

  1. BOOL GetProcessIntegrityLevel(LPSTR szIntegrityLevel, DWORD dwPID);

In 1st state my program working properly and changes the LPSTR szIntegrityLevel But in 2nd, It doesn't work and LPSTR szIntegrityLevel is always empty with any pid.


I created a c dll library in which I declared the following c function that get Integrity Level of a process through a pid :

#define         MAX_INTEGRITY_LEVEL_LENGTH                  30
extern "C"
{
    __declspec(dllexport) BOOL GetProcessIntegrityLevel(DWORD dwPID, LPSTR szIntegrityLevel);
}

C function implementation :

BOOL GetProcessIntegrityLevel(DWORD dwPID, LPSTR szIntegrityLevel)
{
    BOOL    bFlag = FALSE;                      // Return flag
    HANDLE  hToken = INVALID_HANDLE_VALUE;      // Token handle
    HANDLE  hProcess = NULL;                    // Process handle
    BOOL    bProcToken = FALSE;                 // OpenProcessToken() result
    BOOL    bTokenInfo = FALSE;                 // GetTokenInformation() result
    DWORD   dwLengthNeeded = 0;                 // Token information length
    DWORD   dwError = ERROR_SUCCESS;            // GetTokenInformation() last error
    DWORD   dwIntegrityLevel = 0;               // Integrity level

    PTOKEN_MANDATORY_LABEL pTIL = NULL;         // Use as token information

    // Open the process
    hProcess = OpenProcess(MAXIMUM_ALLOWED | PROCESS_QUERY_LIMITED_INFORMATION,
        FALSE, dwPID);
    if (hProcess != NULL)
    {
        // Open process token
        bProcToken = OpenProcessToken(hProcess, TOKEN_QUERY, &hToken);
        if (bProcToken == TRUE)
        {
            // Get token information
            bTokenInfo = GetTokenInformation(hToken, TokenIntegrityLevel,
                NULL, 0, &dwLengthNeeded);
            if (bTokenInfo == FALSE)
            {
                dwError = GetLastError();
                if (dwError == ERROR_INSUFFICIENT_BUFFER)
                {
                    pTIL = (PTOKEN_MANDATORY_LABEL)LocalAlloc(0, dwLengthNeeded);
                    if (pTIL != NULL)
                    {
                        // Get token information
                        bTokenInfo = GetTokenInformation(hToken, TokenIntegrityLevel,
                            pTIL, dwLengthNeeded, &dwLengthNeeded);
                        if (bTokenInfo == TRUE)
                        {
                            // Get integrity level
                            dwIntegrityLevel = *GetSidSubAuthority(pTIL->Label.Sid,
                                (DWORD)(UCHAR)(*GetSidSubAuthorityCount(pTIL->Label.Sid) - 1));

                            if (dwIntegrityLevel <= SECURITY_MANDATORY_LOW_RID)
                            {
                                // Low Integrity
                                StrCpyA(szIntegrityLevel, "Low");
                            }
                            else if ((dwIntegrityLevel >= SECURITY_MANDATORY_MEDIUM_RID) &&
                                (dwIntegrityLevel < SECURITY_MANDATORY_HIGH_RID))
                            {
                                // Medium Integrity
                                StrCpyA(szIntegrityLevel, "Medium");
                            }
                            else if ((dwIntegrityLevel >= SECURITY_MANDATORY_HIGH_RID) &&
                                (dwIntegrityLevel < SECURITY_MANDATORY_SYSTEM_RID))
                            {
                                // High Integrity
                                StrCpyA(szIntegrityLevel, "High");
                            }
                            else if (dwIntegrityLevel >= SECURITY_MANDATORY_SYSTEM_RID)
                            {
                                // System Integrity
                                StrCpyA(szIntegrityLevel, "System");
                            }
                            else if (dwIntegrityLevel == SECURITY_MANDATORY_UNTRUSTED_RID)
                            {
                                // Untrusted integrity
                                StrCpyA(szIntegrityLevel, "Untrusted");
                            }
                            else
                            {
                                StrCpyA(szIntegrityLevel, "UnKnown");
                            }

                            bFlag = TRUE;
                        }

                        LocalFree(pTIL);
                    }
                }
            }

            // Close token handle
            CloseHandle(hToken);
        }

        // Close the process handle
        CloseHandle(hProcess);
    }

    return bFlag;
}

So, I import GetProcessIntegrityLevel() from my c dll in c# as following :

// Define function pointers for using of c dll functions
[DllImport("ProcessesPropDll.dll",CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
[return: MarshalAs(UnmanagedType.Bool)]
// Get integrity level
static extern bool GetProcessIntegrityLevel(ulong dwPID, StringBuilder szIntegrityLevel);

And call the function in c# like this :

// Integrity level
StringBuilder integrityLevel = new StringBuilder(200);

if(GetProcessIntegrityLevel(11684, integrityLevel) == true)
{
      MessageBox.Show(integrityLevel.ToString());
}

In this state when i run the program, the GetProcessIntegrityLevel() returns true but integrityLevel is always empty in messagebox for any pid !!!!

‌Surprisingly, when I interchange the parameters in my c function and also in c# , It does work :

__declspec(dllexport) BOOL GetProcessIntegrityLevel(LPSTR szIntegrityLevel, DWORD dwPID);

This is very strange for me. Also I tried with MarshalAs(UnmanagedType.LPStr)] but give me the same result.

In C# ulong is a 64 bit type. The PID parameter is DWORD which is a 32 bit parameter.

Change your C# parameter declaration from ulong to uint to fix the problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM