Skip to main content
AI Assist is now on Stack Overflow. Start a chat to get instant answers from across the network. Sign up to save and share your chats.
added 16 characters in body
Source Link
PaeneInsula
  • 2.1k
  • 3
  • 34
  • 59

I am trying to learn enough C# so that I can pass a strcture by reference to a C DLL; but it never gets to the "cFunction". As you can see in the cFunction, I am explicitly setting the streamSubset value to 44; but back in the c# portion it does not return "44". Here is the C code:

typedef struct s_mPlot { double price[100]; int streamSubset; } mPlot; extern "C" __declspec( dllexport ) void cFunction(mPlot *Mplot){ Mplot->streamSubset = 44;} 

// and here is the c# code using System; using Vibe.Function; using System.Runtime.InteropServices;

 using System; using Vibe.Function; using System.Runtime.InteropServices;  [StructLayout(LayoutKind.Sequential)] public class MPLOT { [MarshalAs(UnmanagedType.ByValArray, SizeConst = 100)] public double [] price; public int streamSubset; } namespace Vibe.Indicator{ public class myIdx : IndicatorObject { [DllImport("C:\\Users\\joe\\mcDll.dll", CharSet = CharSet.Auto)] public static extern void cFunction( [In, MarshalAs(UnmanagedType.LPStruct)] MPLOT mPlot ); public myIdx(object _ctx):base(_ctx){} private IPlotObject plot1; protected override void Create() { MPLOT mPlot = new MPLOT(); mPlot.streamSubset = 2; cFunction(mPlot); if (mPlot.streamSubset == 44) go(); } } 

}

I am trying to learn enough C# so that I can pass a strcture by reference to a C DLL; but it never gets to the "cFunction". As you can see in the cFunction, I am explicitly setting the streamSubset value to 44; but back in the c# portion it does not return "44". Here is the C code:

typedef struct s_mPlot { double price[100]; int streamSubset; } mPlot; extern "C" __declspec( dllexport ) void cFunction(mPlot *Mplot){ Mplot->streamSubset = 44;} 

// and here is the c# code using System; using Vibe.Function; using System.Runtime.InteropServices;

 [StructLayout(LayoutKind.Sequential)] public class MPLOT { [MarshalAs(UnmanagedType.ByValArray, SizeConst = 100)] public double [] price; public int streamSubset; } namespace Vibe.Indicator{ public class myIdx : IndicatorObject { [DllImport("C:\\Users\\joe\\mcDll.dll", CharSet = CharSet.Auto)] public static extern void cFunction( [In, MarshalAs(UnmanagedType.LPStruct)] MPLOT mPlot ); public myIdx(object _ctx):base(_ctx){} private IPlotObject plot1; protected override void Create() { MPLOT mPlot = new MPLOT(); mPlot.streamSubset = 2; cFunction(mPlot); if (mPlot.streamSubset == 44) go(); } } 

}

I am trying to learn enough C# so that I can pass a strcture by reference to a C DLL; but it never gets to the "cFunction". As you can see in the cFunction, I am explicitly setting the streamSubset value to 44; but back in the c# portion it does not return "44". Here is the C code:

typedef struct s_mPlot { double price[100]; int streamSubset; } mPlot; extern "C" __declspec( dllexport ) void cFunction(mPlot *Mplot){ Mplot->streamSubset = 44;} 

// and here is the c# code

 using System; using Vibe.Function; using System.Runtime.InteropServices;  [StructLayout(LayoutKind.Sequential)] public class MPLOT { [MarshalAs(UnmanagedType.ByValArray, SizeConst = 100)] public double [] price; public int streamSubset; } namespace Vibe.Indicator{ public class myIdx : IndicatorObject { [DllImport("C:\\Users\\joe\\mcDll.dll", CharSet = CharSet.Auto)] public static extern void cFunction( [In, MarshalAs(UnmanagedType.LPStruct)] MPLOT mPlot ); public myIdx(object _ctx):base(_ctx){} private IPlotObject plot1; protected override void Create() { MPLOT mPlot = new MPLOT(); mPlot.streamSubset = 2; cFunction(mPlot); if (mPlot.streamSubset == 44) go(); } } 

}

Source Link
PaeneInsula
  • 2.1k
  • 3
  • 34
  • 59

passing structure from c# to C dll

I am trying to learn enough C# so that I can pass a strcture by reference to a C DLL; but it never gets to the "cFunction". As you can see in the cFunction, I am explicitly setting the streamSubset value to 44; but back in the c# portion it does not return "44". Here is the C code:

typedef struct s_mPlot { double price[100]; int streamSubset; } mPlot; extern "C" __declspec( dllexport ) void cFunction(mPlot *Mplot){ Mplot->streamSubset = 44;} 

// and here is the c# code using System; using Vibe.Function; using System.Runtime.InteropServices;

 [StructLayout(LayoutKind.Sequential)] public class MPLOT { [MarshalAs(UnmanagedType.ByValArray, SizeConst = 100)] public double [] price; public int streamSubset; } namespace Vibe.Indicator{ public class myIdx : IndicatorObject { [DllImport("C:\\Users\\joe\\mcDll.dll", CharSet = CharSet.Auto)] public static extern void cFunction( [In, MarshalAs(UnmanagedType.LPStruct)] MPLOT mPlot ); public myIdx(object _ctx):base(_ctx){} private IPlotObject plot1; protected override void Create() { MPLOT mPlot = new MPLOT(); mPlot.streamSubset = 2; cFunction(mPlot); if (mPlot.streamSubset == 44) go(); } } 

}