Hi everyone, I have a hopefully simple problem. I've written a dll in C++ (unmanaged, UNICODE). The dll takes care of all of the threading and the memory management. The format of my C++ struct is:
I have a CreateParam function that takes a NULL pointer from a client program and supplies it with an address and default values. In C++ code (which works) it looks likeCode:struct OSDParam{ wchar_t* userfont; int fontsize; int Monitor; int Displength; int alpha; wchar_t* text; int Coloring[3]; float x; float y; int fontstyle; HANDLE procHand; OSDParam() { userfont = L"Times New Roman"; fontsize = 45; Monitor = 2; Displength = 5; alpha = 255; text = L"No text set for OSD"; Coloring[0]= 0; Coloring[1] = 0; Coloring[2] = 0; x = 0.0; y = 0.0; fontstyle = 0; HANDLE procHand = NULL; } };
This works very well. Now, I'm trying to create a client program in C# and I can't seem to provide an address for the struct. It runs, but the values passed to the client are incorrect and frequently ridiculous. This would seem to imply that I'm just randomly accessing memory and not assigning it like one does in C++. The test code follows below. If anyone has any ideas how I can assign the address through the CreateParam function, please let me know.Code:OSDParam* params = NULL; CreateParam(params);
~Steve
Code:using System; using System.Runtime.InteropServices; using System.Text; namespace ConsoleApplication1 { [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] public struct OSDParam { public String userfont; public int fontsize; public int Monitor; public int Displength; public int alpha; public String text; public int[] Coloring; public float x; public float y; public int fontstyle; } /// <summary> /// Summary description for Class1. /// </summary> /// public class OSDdll { [DllImport("OSD.dll")] public static extern int InitOSD(); [DllImport("OSD.dll")] public static extern int ExitOSD(); [DllImport("OSD.dll")] public static extern int CreateParam(ref OSDParam Parameters); [DllImport("OSD.dll")] public static extern int SemiPermText(OSDParam Parameters); } class Class1 { [STAThread] static void Main(string[] args) { OSDParam t = new OSDParam(); OSDdll.InitOSD(); //Pass the address to the struct that my dll manages OSDdll.CreateParam(ref t); Console.WriteLine(t.fontsize); OSDdll.ExitOSD(); } } }




Reply With Quote