Look, we don’t have your DLL or the potential FPGA hardware behind that library. So we can’t really help you much. The documentation you provided is very basic and doesn’t really go much into detail. However if you have trouble defining proper external definitions for that interface, here’s how I would interpret the document and header you shared:
public static class FPGAInterface
{
const string dllName = "YourDllName.dll";
[DllImport(dllName)]
private static extern byte _GetFPGAOCount(ref byte ucCount);
[DllImport(dllName)]
private static extern byte _SetFPGAOIdx(uint uIdx, byte bFlag);
[DllImport(dllName)]
private static extern byte _SetFPGAOAll(byte bFlag);
[DllImport(dllName)]
private static extern byte _SetFPGAOutput(uint ulData1, uint ulData2);
[DllImport(dllName)]
private static extern byte _GetFPGAOIdx(uint uIdx, ref byte bFlag);
[DllImport(dllName)]
private static extern byte _GetFPGAOutput(ref uint ulData1, ref uint ulData2);
[DllImport(dllName)]
private static extern byte _GetFPGAICount(ref byte ucCount);
[DllImport(dllName)]
private static extern byte _GetFPGAIIdx(uint uIdx, ref byte bFlag);
[DllImport(dllName)]
private static extern byte _GetFPGAInput(ref uint ulData1, ref uint ulData2);
[DllImport(dllName)]
private static extern byte _SetFPGAIHWDebTime(uint uIdxGroup, uint time);
public static bool GetFPGAOCount(ref byte ucCount) => _GetFPGAOCount(ref ucCount) != 0;
public static bool SetFPGAOIdx(uint uIdx, bool bFlag) => _SetFPGAOIdx(uIdx, bFlag ? 1 : 0) != 0;
public static bool SetFPGAOAll(bool bFlag) => _SetFPGAOAll(bFlag ? 1 : 0) != 0;
public static bool SetFPGAOutput(ulong ulData) => _SetFPGAOutput((uint)(ulData & 0xFFFFFFFF), (uint)((ulData>>32) & 0xFFFFFFFF)) != 0;
public static bool GetFPGAOIdx(uint uIdx, ref bool bFlag)
{
byte flag = 0;
if (_GetFPGAOIdx(uIdx, ref flag) == 0)
return false;
bFlag = flag != 0;
return true;
}
public static bool GetFPGAOutput(ref ulong ulData)
{
uint d1 = 0, d2 = 0;
if (_GetFPGAOutput(ref d1, ref d2) == 0)
return false;
ulData = (ulong)d1 | (ulong)d2 << 32;
return true;
}
public static bool GetFPGAICount(ref byte ucCount) => _GetFPGAICount(ref ucCount) != 0;
public static bool GetFPGAIIdx(uint uIdx, ref bool bFlag)
{
byte flag = 0;
if (_GetFPGAIIdx(uIdx, ref flag) == 0)
return false;
bFlag = flag != 0;
return true;
}
public static bool GetFPGAInput(ref ulong ulData)
{
uint d1 = 0, d2 = 0;
if (_GetFPGAInput(ref d1, ref d2) == 0)
return false;
ulData = (ulong)d1 | (ulong)d2 << 32;
return true;
}
public static bool SetFPGAIHWDebTime(uint uIdxGroup, uint time) => _SetFPGAIHWDebTime(uIdxGroup, time) != 0;
}
I seperated the actual external declaration from the interface in C#. So all the external methods are declared private and essentially wrapped by the public methods. At this point we can convert the byte return type into an actual bool in C#. Likewise I converted the other bool types and also the seperate 32 bit read and write methods now work with a 64 bit ulong in C#. Though if you need them as two seperate 32 bit uints, that’s an easy change.
Currently I kept the bool return type for all methods and the actual data is pass via parameters as we don’t know what it means for a method to “fail”. If that can happen often, it’s better this way. However if it should usually never fail, it might be better to throw an exception in case the method fails.
Interface with exceptions
public static class FPGAInterfaceEx
{
public class EFPGAException : System.Exception
{
public EFPGAException(string aMessage) : base(aMessage) { }
}
const string dllName = "YourDllName.dll";
[DllImport(dllName)]
private static extern byte _GetFPGAOCount(ref byte ucCount);
[DllImport(dllName)]
private static extern byte _SetFPGAOIdx(uint uIdx, byte bFlag);
[DllImport(dllName)]
private static extern byte _SetFPGAOAll(byte bFlag);
[DllImport(dllName)]
private static extern byte _SetFPGAOutput(uint ulData1, uint ulData2);
[DllImport(dllName)]
private static extern byte _GetFPGAOIdx(uint uIdx, ref byte bFlag);
[DllImport(dllName)]
private static extern byte _GetFPGAOutput(ref uint ulData1, ref uint ulData2);
[DllImport(dllName)]
private static extern byte _GetFPGAICount(ref byte ucCount);
[DllImport(dllName)]
private static extern byte _GetFPGAIIdx(uint uIdx, ref byte bFlag);
[DllImport(dllName)]
private static extern byte _GetFPGAInput(ref uint ulData1, ref uint ulData2);
[DllImport(dllName)]
private static extern byte _SetFPGAIHWDebTime(uint uIdxGroup, uint time);
public static byte GetFPGAOCount()
{
byte count = 0;
if (_GetFPGAOCount(ref count) == 0)
throw new EFPGAException($"{nameof(GetFPGAICount)} failed");
return count;
}
public static void SetFPGAOIdx(uint uIdx, bool bFlag)
{
if (_SetFPGAOIdx(uIdx, bFlag ? 1 : 0) == 0)
throw new EFPGAException($"{nameof(SetFPGAOIdx)} failed");
}
public static void SetFPGAOAll(bool bFlag)
{
if (_SetFPGAOAll(bFlag ? 1 : 0) == 0)
throw new EFPGAException($"{nameof(SetFPGAOAll)} failed");
}
public static void SetFPGAOutput(ulong ulData)
{
if (_SetFPGAOutput((uint)(ulData & 0xFFFFFFFF), (uint)((ulData >> 32) & 0xFFFFFFFF)) == 0)
throw new EFPGAException($"{nameof(SetFPGAOutput)} failed");
}
public static bool GetFPGAOIdx(uint uIdx)
{
byte flag = 0;
if (_GetFPGAOIdx(uIdx, ref flag) == 0)
throw new EFPGAException($"{nameof(GetFPGAOIdx)} failed");
return flag != 0;
}
public static ulong GetFPGAOutput()
{
uint d1 = 0, d2 = 0;
if (_GetFPGAOutput(ref d1, ref d2) == 0)
throw new EFPGAException($"{nameof(GetFPGAOutput)} failed");
return (ulong)d1 | (ulong)d2 << 32;
}
public static byte GetFPGAICount()
{
byte count = 0;
if (_GetFPGAICount(ref count) == 0)
throw new EFPGAException($"{nameof(GetFPGAICount)} failed");
return count;
}
public static bool GetFPGAIIdx(uint uIdx)
{
byte flag = 0;
if (_GetFPGAIIdx(uIdx, ref flag) == 0)
throw new EFPGAException($"{nameof(GetFPGAIIdx)} failed");
return flag != 0;
}
public static ulong GetFPGAInput()
{
uint d1 = 0, d2 = 0;
if (_GetFPGAInput(ref d1, ref d2) == 0)
throw new EFPGAException($"{nameof(GetFPGAInput)} failed");
return (ulong)d1 | (ulong)d2 << 32;
}
public static void SetFPGAIHWDebTime(uint uIdxGroup, uint time)
{
if (_SetFPGAIHWDebTime(uIdxGroup, time) == 0)
throw new EFPGAException($"{nameof(SetFPGAIHWDebTime)} failed");
}
Of course I can not test any of this. Also your post number #4 contained those Init and Uninit methods which seems to be relevant for WinXP only. Though if this is a really old library, it’s possible that it’s required from WinXP onwards since win XP had stricter direct IO. Though those are all just assumptions and it’s pointless to speculate.