Posts

Showing posts from September, 2024

RoadieRichStateMachine

A while back, I found myself needing a state machine for a project. I had some experience with a state machine from a previous employment, but I couldn't find one that looked like that. So I wrote one. If you clone the repo, you'll get a demo project and unittests, but I also wanted to figure out Nuget, so I uploaded it there .  The Nuget package doesn't include any documentation other than that used for IntelliSense, so I figured I write about it a little.

Marshalling Output Arrays

I recently ran into an issue getting values out of a function that you pass an int* to get an array out. The C function looks like this: int __stdcall UP_GetProgList(int prog_type, int *sn_list, int count, int *count_returned) Naively, I wrote the following P/Invoke call: [DllImport(@"the.dll")] private static extern int UP_GetProgList(int prog_type, out int[] sn_list, int count, out int count_returned); Turns out that when I called that, I got an access violation. I tried searching for the solution, I even asked on StackOverflow, but no one seemed to have the answer in an easy-to-digest format. So I did some digging. My first attempt used out int instead of the array. That didn't give me an exception, but it would only allow me to get one value out. I eventually found the IntPtr type. My first attempt at using it looked something like this: void Main() { var listP = Marshal.AllocHGlobal(20); UP_GetProgList(2, listP, 20, out int countReturned); f...