I have a C api and I am using p/invoke to call a function from the api in my C# application. The function signature is:
int APIENTRY GetData (CASHTYPEPOINTER cashData);
Type definitions:
typedef CASHTYPE* CASHTYPEPOINTER;
typedef struct CASH
{
int CashNumber;
CURRENCYTYPE Types[24];
} CASHTYPE;
typedef struct CURRENCY
{
char Name[2];
char NoteType[6];
int NoteNumber;
} CURRENCYTYPE;
How would be my C# method signature and data types?
Thank you.
You need to specify the array sizes using SizeConst:
using System;
using System.Runtime.InteropServices;
public static class MyCApi
{
[StructLayout(LayoutKind.Sequential)]
public struct CASHTYPE
{
public int CashNumber;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 24)]
public CURRENCYTYPE[] Types;
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public struct CURRENCYTYPE
{
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 2)]
public string Name;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 6)]
public string NoteType;
public int NoteNumber;
}
[DllImport("MyCApi.dll")]
public static extern int GetData(ref CASHTYPE cashData);
}
I think it may look like this
using System.Runtime.InteropServices;
[StructLayout(LayoutKind.Sequential)]
public struct CASH{
public int CashNumber;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 24)]
public CURRENCY Types[24];
}
[ StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi)]
public struct CURRENCY {
[MarshalAs( UnmanagedType.ByValTStr, SizeConst=2 )]
public string Name;
[MarshalAs( UnmanagedType.ByValTStr, SizeConst=6 )]
public string NoteType;
public int NoteNumber;
}
class Wrapper {
[DllImport("my.dll")]
public static extern int GetData(ref CASH cashData}
}
Related
I am using NVAPI.dll to change gpu's core clock. using NvAPI_GPU_SetPstates20 function in nvapi.dll
The struct in C# for nvapi.dll is this:
[StructLayout(LayoutKind.Explicit, Pack = 8)]
internal struct dataStruct
{
[FieldOffset(0)]
public singleStruct single;
[FieldOffset(0)]
public rangeStruct range;
}
[StructLayout(LayoutKind.Sequential, Pack = 8)]
internal struct valuerange
{
public int min;
public int max;
}
[StructLayout(LayoutKind.Sequential, Pack = 8)]
internal struct NV_GPU_PSTATE20_BASE_VOLTAGE_ENTRY
{
public uint domainId;
public uint editable;//1
public uint reserved;//31
public uint voltageUV;
public NV_GPU_PERF_PSTATES20_PARAM_DELTA voltageDeltaUV;
}
[StructLayout(LayoutKind.Sequential, Pack = 8)]
internal struct NV_GPU_PERF_PSTATES20_PARAM_DELTA
{
public int value;
public valuerange valueRanges;
}
[StructLayout(LayoutKind.Sequential, Pack = 8)]
internal struct pstateStruct
{
public uint pStateId;
public uint editable;//1
public uint reserved;//31
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
public NV_GPU_PSTATE20_CLOCK_ENTRY[]clocks;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)]
public NV_GPU_PSTATE20_BASE_VOLTAGE_ENTRY[]baseVoltages;
}
[StructLayout(LayoutKind.Sequential, Pack = 8)]
internal struct singleStruct
{
public uint frequencyKHz;
}
[StructLayout(LayoutKind.Sequential, Pack = 8)]
internal struct rangeStruct
{
public uint minFrequencyKHz;
public uint maxFrequencyKHz;
public uint domainId;
public uint minVoltageUV;
public uint maxVoltageUV;
}
[StructLayout(LayoutKind.Sequential, Pack = 8)]
internal struct overVoltageStruct
{
public uint numVoltages;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)]
public NV_GPU_PSTATE20_BASE_VOLTAGE_ENTRY []voltages;//[4]
}
[StructLayout(LayoutKind.Sequential, Pack = 8)]
internal struct NV_GPU_PSTATE20_CLOCK_ENTRY
{
public uint domainId;
public uint typeId;
public uint editable;//1
public uint reserved;//31
public NV_GPU_PERF_PSTATES20_PARAM_DELTA frequencyDeltaKHz;
public dataStruct data;
}
[StructLayout(LayoutKind.Sequential, Pack = 8)]
internal struct NV_GPU_PERF_PSTATES20_INFO
{
public uint version;
public uint editable;//1
public uint reserved;//31
public uint numPStates;
public uint numClocks;
public uint numBaseVoltages;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)]
public pstateStruct[] pStates;
public overVoltageStruct overVoltage;
}
This is my function:
public static void SetCoreClock(NvPhysicalGpuHandle handle,int clock)
{
NV_GPU_PERF_PSTATES20_INFO pStatesInfo=new NV_GPU_PERF_PSTATES20_INFO();
pStatesInfo.version = (uint) Marshal.SizeOf(typeof(NV_GPU_PERF_PSTATES20_INFO)) | 0x10000;
pStatesInfo.numPStates = 1;
pStatesInfo.numClocks = 1;
pStatesInfo.editable = 1;
pStatesInfo.reserved = 31;
pStatesInfo.pStates = new pstateStruct[16];
pStatesInfo.overVoltage.voltages = new NV_GPU_PSTATE20_BASE_VOLTAGE_ENTRY[4];
pStatesInfo.pStates[0].baseVoltages = new NV_GPU_PSTATE20_BASE_VOLTAGE_ENTRY[4];
pStatesInfo.pStates[0].clocks = new NV_GPU_PSTATE20_CLOCK_ENTRY[8];
pStatesInfo.pStates[0].clocks[0].editable = 1;
pStatesInfo.pStates[0].clocks[0].reserved = 31;
pStatesInfo.pStates[0].pStateId = 0;
pStatesInfo.pStates[0].clocks[0].domainId = 0;
pStatesInfo.pStates[0].clocks[0].typeId = 0;
pStatesInfo.pStates[0].clocks[0].frequencyDeltaKHz.value = clock;
pStatesInfo.pStates[0].clocks[0].frequencyDeltaKHz.valueRanges.max = 0;
pStatesInfo.pStates[0].clocks[0].frequencyDeltaKHz.valueRanges.min = 0;
a = NVAPI.NvAPI_GPU_SetPstates20(handle,ref pStatesInfo);
}
when I run the function "SetCoreClock". The value a return "INCOMPATIBLE_STRUCT_VERSION".
Is my struct wrong?
I found a C++ code. the code's struct is:
struct NV_GPU_PERF_PSTATES20_PARAM_DELTA
{
int value;
struct
{
int min;
int max;
} valueRange;
};
struct NV_GPU_PSTATE20_BASE_VOLTAGE_ENTRY
{
unsigned int domainId;
unsigned int editable : 1;
unsigned int reserved : 31;
unsigned int voltageUV;
NV_GPU_PERF_PSTATES20_PARAM_DELTA voltageDeltaUV;
};
struct NV_GPU_PSTATE20_CLOCK_ENTRY
{
unsigned int domainId;
unsigned int typeId;
unsigned int editable : 1;
unsigned int reserved : 31;
NV_GPU_PERF_PSTATES20_PARAM_DELTA frequencyDeltaKHz;
union
{
struct
{
unsigned int frequencyKHz;
} single;
struct
{
unsigned int minFrequencyKHz;
unsigned int maxFrequencyKHz;
unsigned int domainId;
unsigned int minVoltageUV;
unsigned int maxVoltageUV;
} range;
} data;
};
struct NV_GPU_PERF_PSTATES20_INFO
{
unsigned int version;
unsigned int editable : 1;
unsigned int reserved : 31;
unsigned int numPStates;
unsigned int numClocks;
unsigned int numBaseVoltages;
struct
{
unsigned int pStateId;
unsigned int editable : 1;
unsigned int reserved : 31;
NV_GPU_PSTATE20_CLOCK_ENTRY clocks[8];
NV_GPU_PSTATE20_BASE_VOLTAGE_ENTRY baseVoltages[4];
} pStates[16];
struct
{
unsigned int numVoltages;
NV_GPU_PSTATE20_BASE_VOLTAGE_ENTRY voltages[4];
} overVoltage;
};```
Thanks for helping me solved the problem.
it means you have the struct the incorrect size. a example I found said the version should be 0x11c94. things like reserved IMHO need to be removed. I had same issue and I did this to find the version:
uint GPUVER3 = (uint)Marshal.SizeOf(typeof(NV_GPU_PERF_PSTATES20_INFO_V1)) | 0x20000;
Once you get the version the same as 0x11c94 it should work.
C code I used to work this out
https://github.com/suppai/nvidia-overclock
I have been trying to convert this struct to C# for the last couple of days, but to no avail...
https://msdn.microsoft.com/en-us/library/windows/desktop/bb736369(v=vs.85).aspx
typedef struct _WTSCLIENT {
TCHAR ClientName[CLIENTNAME_LENGTH + 1];
TCHAR Domain[DOMAIN_LENGTH + 1 ];
TCHAR UserName[USERNAME_LENGTH + 1];
TCHAR WorkDirectory[MAX_PATH + 1];
TCHAR InitialProgram[MAX_PATH + 1];
BYTE EncryptionLevel;
ULONG ClientAddressFamily;
USHORT ClientAddress[CLIENTADDRESS_LENGTH + 1];
USHORT HRes;
USHORT VRes;
USHORT ColorDepth;
TCHAR ClientDirectory[MAX_PATH + 1];
ULONG ClientBuildNumber;
ULONG ClientHardwareId;
USHORT ClientProductId;
USHORT OutBufCountHost;
USHORT OutBufCountClient;
USHORT OutBufLength;
TCHAR DeviceId[MAX_PATH + 1];
} WTSCLIENT, *PWTSCLIENT;
This is my attempt so far:
[StructLayout( LayoutKind.Sequential )]
public struct _WTSCLIENT {
[MarshalAs( UnmanagedType.LPTStr )]
public String ClientName;
[MarshalAs( UnmanagedType.LPTStr )]
public String Domain;
[MarshalAs( UnmanagedType.LPTStr )]
public String UserName;
[MarshalAs( UnmanagedType.LPTStr )]
public String WorkDirectory;
[MarshalAs( UnmanagedType.LPTStr )]
public String InitialProgram;
public Byte EncryptionLevel;
public uint ClientAddressFamily;
[MarshalAsAttribute( UnmanagedType.ByValArray)]
public byte[] ClientAddress;
public ushort HRes;
public ushort VRes;
public ushort ColorDepth;
[MarshalAs( UnmanagedType.LPTStr )]
public String ClientDirectory;
public uint ClientBuildNumber;
public uint ClientHardwareId;
public ushort ClientProductId;
public ushort OutBufCountHost;
public ushort OutBufCountClient;
public ushort OutBufLength;
[MarshalAs( UnmanagedType.LPTStr )]
public String DeviceId;
}
I use it like this:
WTSQuerySessionInformation( serverHandle, si.SessionID, WTS_INFO_CLASS.WTSClientInfo, out clientInfoPtr, out bytes );
clientInfo = (_WTSCLIENT)Marshal.PtrToStructure( clientInfoPtr, typeof( _WTSCLIENT ) );
...but it bombs out silently, so obviously I have not matched the structure...
Can anyone help me with the struct conversion to C#
TIA...
--
Dag.
Try
[StructLayout(LayoutKind.Sequential)]
public struct WTSCLIENT
{
private const int CLIENTNAME_LENGTH = 20;
private const int DOMAIN_LENGTH = 17;
private const int USERNAME_LENGTH = 20;
private const int MAX_PATH = 260;
private const int CLIENTADDRESS_LENGTH = 30;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = CLIENTNAME_LENGTH + 1)]
public string ClientName;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = DOMAIN_LENGTH + 1)]
public string Domain;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = USERNAME_LENGTH + 1)]
public string UserName;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = MAX_PATH + 1)]
public string WorkDirectory;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = MAX_PATH + 1)]
public string InitialProgram;
public byte EncryptionLevel;
public uint ClientAddressFamily;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = CLIENTADDRESS_LENGTH + 1)]
public ushort[] ClientAddress;
public ushort HRes;
public ushort VRes;
public ushort ColorDepth;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = MAX_PATH + 1)]
public string ClientDirectory;
public uint ClientBuildNumber;
public uint ClientHardwareId;
public ushort ClientProductId;
public ushort OutBufCountHost;
public ushort OutBufCountClient;
public ushort OutBufLength;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = MAX_PATH + 1)]
public string DeviceId;
}
It should work. Its Marshal.SizeOf is the "right" one, both at 32 and 64 bits, both Ansi and Unicode.
If you are using the Unicode version of the methods change the first line to:
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
You could try the P/Invoke Interop Assistant
For your ULONG ClientBuildNumber; and all other ULONG s in C++, you have used the C# type uint. It could be an error that your definition of ULONG is a different size than uint.
Maybe you can try using ulong in C# (unsigned 64-bit, 0 to 18,446,744,073,709,551,615). This should match the standard Microsoft C++ ULONG definition.
MSDN C++
MSDN C# ulong
hi everyone i need to make the time zone for my windows CE 6 changed to
"(GMT +03:00) Kuwait, Riyadh"
and this my code
[DllImport("coredll.dll", CharSet = CharSet.Auto)]
private static extern int GetTimeZoneInformation(out TimeZoneInformation lpTimeZoneInformation);
[DllImport("coredll.dll", CharSet = CharSet.Auto)]
private static extern bool SetTimeZoneInformation(ref TimeZoneInformation lpTimeZoneInformation);
[StructLayout(LayoutKind.Sequential)]
public struct SYSTEMTIME
{
public int wYear;
public int wMonth;
public int wDayOfWeek;
public int wDay;
public int wHour;
public int wMinute;
public int wSecond;
public int wMilliseconds;
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct TimeZoneInformation
{
public int bias;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
public string standardName;
public SYSTEMTIME standardDate;
public int standardBias;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
public string daylightName;
public SYSTEMTIME daylightDate;
public int daylightBias;
}
how can I use this code to set the time zone for the specific one
please any one can help me.
I found the solution by adding a registry file with this script.
[HKEY_LOCAL_MACHINE\Time Zones]
"Default"="Arab Standard Time"
How to Marshal a:
[C++]
#define MAX_STRING_SIZE 255
typedef char T_STRING[MAX_STRING_SIZE];
typedef struct
{
unsigned long m_ID;
T_STRING m_name;
} Result;
In C#?
Currently I am doing this (but it does not work):
[C#]
[StructLayout(LayoutKind.Sequential)]
public struct Result
{
public uint m_ID;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 255)]
public char[] m_name;
}
I have tried to use a IntPtr instead of char[] with equal non-working result. Both with and without [MarshalAs(...)].
Marshal it like this:
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public struct Result
{
public uint m_ID;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 255)]
public string m_name;
}
I've spent a lot of time to look for the solution but still don't find it out.
I have 2 classes:
[StructLayout(LayoutKind.Sequential)]
public class Result
{
public int Number;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 4)]
public string Name;
public int Size;
}
[StructLayout(LayoutKind.Sequential)]
public class CoverObject
{
public int NumOfResults;
[MarshalAs(UnmanagedType.ByValArray, ArraySubType = UnmanagedType.Struct, SizeConst = 4)]
public Result[] Results;
}
My expectation that the command Marshal.SizeOf(typeof(CoverObject)) will return 52, but not, it's just 20. Thus, all of marshall and unmarshall that I use later are not working.
Seeming it only counts the first member (Number) in Result class. Did I do anything wrong?
Change your classes to structs
[StructLayout(LayoutKind.Sequential)]
public struct Result
{
public int Number;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 4)]
public string Name;
public int Size;
}
[StructLayout(LayoutKind.Sequential)]
public struct CoverObject
{
public int NumOfResults;
[MarshalAs(UnmanagedType.ByValArray, ArraySubType = UnmanagedType.Struct, SizeConst = 4)]
public Result[] Results;
}
some where else:
Marshal.SizeOf(typeof(CoverObject)) // it will return 52