Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
i have this function in c++ which defined in header file

C++
MYDLL_API BOOL GetFolderSize(LPCTSTR lpszStartFolder, 
               BOOL bRecurse, 
               BOOL bQuickSize,
               PLARGE_INTEGER lpFolderSize,
               LPDWORD lpFolderCount /*= NULL*/,
               LPDWORD lpFileCount /*= NULL*/);


where MYDLL_API is defined as

C++
#ifdef MYDLL_EXPORTS
#define MYDLL_API __declspec(dllexport)
#else
#define MYDLL_API __declspec(dllimport)
#endif



after compile it as DLL project
i create c# declaration for this function as this


C#
[StructLayout(LayoutKind.Explicit, Size = 8)]
    struct LARGE_INTEGER
    {
        [FieldOffset(0)]
        public Int64 QuadPart;
        [FieldOffset(0)]
        public UInt32 LowPart;
        [FieldOffset(4)]
        public Int32 HighPart;
    }

  [DllImport("mydll.dll")]
    extern static bool GetFolderSize(string lpszStartFolder,
                               bool bRecurse,
                               bool bQuickSize,
                               out LARGE_INTEGER lpFolderSize,
                                out UInt32 lpFolderCount,
                                out UInt32 lpFileCount);


in run time it did not give an exception but it return false (mean that the function is failed )
why ? is the reason because of complicated data type in c++ (PLARGE_INTEGER)
or what ??
thanks

What I have tried:

i have try this

[StructLayout(LayoutKind.Explicit, Size = 8)]
struct LARGE_INTEGER
{
[FieldOffset(0)]
public Int64 QuadPart;
[FieldOffset(0)]
public UInt32 LowPart;
[FieldOffset(4)]
public Int32 HighPart;
}

[DllImport("mydll.dll")]
extern static bool GetFolderSize(string lpszStartFolder,
bool bRecurse,
bool bQuickSize,
out LARGE_INTEGER lpFolderSize,
out UInt32 lpFolderCount,
out UInt32 lpFileCount);
Posted
Updated 3-Oct-16 8:04am
Comments
Richard MacCutchan 3-Oct-16 13:35pm    
The only way to know the answer is by using the debugger. We do not know what values you are passing in to the function, or why it is returning false.

The library you're calling is using cdecl calling convention, but the default for C# is winapi, a.k.a. stdcall. Change your DllImport call to look like this instead:
C#
[DllImport("mydll.dll"), CallingConvention=CallingConvention.Cdecl)]
extern static bool GetFolderSize(string lpszStartFolder,
    bool bRecurse,
    bool bQuickSize,
    out LARGE_INTEGER lpFolderSize,
    out UInt32 lpFolderCount,
    out UInt32 lpFileCount);
 
Share this answer
 
0) Make double-damn sure your method prototype is correct.

1) Change the "out" parameters to "output".

2) Why are you calling an unmanaged DLL to do that when .Net provides exactly the same functionality?
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900