Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

C# Wrapper Question #2

Open
shaggygi opened this issue Oct 27, 2023 · 0 comments
Open

C# Wrapper Question #2

shaggygi opened this issue Oct 27, 2023 · 0 comments

Comments

@shaggygi
Copy link

Hi Jim (and sorry to bother), but I found the following link you participated in and I had a question related to this for C# you might be able to help.

https://stackoverflow.com/questions/62331489/c-sharp-calling-a-dll-function-that-returns-a-pointer-to-pointer-to-an-array-of

First, let me thank you for providing current tidbits to help the community on related topic. Very helpful so far.

I'm creating a C# wrapper class for the STM32CubProg and was successful using similar code to get a list of connected ST-LINKs. The major hurdle now is setting up basic code to pass the selected DebugConnectParameters to connect with the device. When calling the connect function, it always returns -8 (Invalid Parameters). Below is some basic code I've setup...

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public class DebugConnectParameters
{
    public DebugPort DebugPort;
    public int Index;
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 33)]
    public string SerialNumber;
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 20)]
    public string FirmwareVersion;
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 5)]
    public string TargetVoltage;
    public int AccesPortNumber;
    public int AccessPort;
    public DebugConnectMode ConnectionMode;  
    public DebugResetMode ResetMode;    
    public int IsOldFirmware;    
    public Frequencies Frequencies;    
    public int Frequency;    
    public int IsBridge;    
    public int Shared;
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 100)]
    public string Board;
    public int DebugSleep;
    public int Speed;
}

Stm32CubeProgrammer.Commands.StLink.GetList(false, out Stm32CubeProgrammer.Types.DebugConnectParameters[] stLinkList);
int result = Stm32CubeProgrammer.Commands.StLink.Connect(stLinkList[0]);

public static int Connect(DebugConnectParameters debugParameters)
{
    IntPtr stListPtr = Marshal.AllocHGlobal(Marshal.SizeOf<DebugConnectParameters>());
    Marshal.StructureToPtr(debugParameters, stListPtr, true);
    return NativeCommands.ConnectStLink(stListPtr);
}

[DllImport(Native.CubeProgrammerApiDllFilePath, CallingConvention = CallingConvention.Cdecl, EntryPoint = "connectStLink")]
public static extern int ConnectStLink(IntPtr debugParameters);

Were you successful in passing in the DebugConnectParameters data structure to make a successful connection?

Again, sorry for intruding (especially since this is a Python API). Any help will be appreciated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant