public void BagSaveAndLoad1() { NativeProcedure p1 = new NativeProcedure("p1"); p1.Signature.ReturnType = new NativeBuiltinType(BuiltinType.NativeBoolean); NativeTypeDef td = new NativeTypeDef("LPWSTR", new NativePointer(BuiltinType.NativeWChar)); p1.Signature.Parameters.Add(new NativeParameter("param1", new NativeNamedType("LPWSTR", td))); Assert.Equal("boolean p1(LPWSTR param1)", p1.DisplayName); Assert.Equal("p1(Sig(boolean)(Sal)(param1(LPWSTR(LPWSTR(*(wchar))))(Sal)))", SymbolPrinter.Convert(p1)); var ns = new BasicSymbolStorage(); ns.AddProcedure(p1); ns.AddTypeDef(td); NativeSymbolBag bag = new NativeSymbolBag(ns); NativeProcedure ret1 = null; Assert.True(bag.TryGetGlobalSymbol("p1", out ret1)); bag.AddProcedure(ret1); Assert.True(bag.TryResolveSymbolsAndValues()); Assert.Equal(SymbolPrinter.Convert(p1), SymbolPrinter.Convert(ret1)); }
public void SaveAndLoad6() { NativeFunctionPointer fptr = new NativeFunctionPointer("f1"); Assert.Equal(NativeCallingConvention.WinApi, fptr.CallingConvention); fptr.CallingConvention = NativeCallingConvention.Pascal; fptr.Signature.ReturnType = new NativeBuiltinType(BuiltinType.NativeChar); NativeProcedure proc = new NativeProcedure("p1"); Assert.Equal(NativeCallingConvention.WinApi, proc.CallingConvention); proc.CallingConvention = NativeCallingConvention.CDeclaration; proc.Signature.ReturnType = new NativeBuiltinType(BuiltinType.NativeChar); var ns = new BasicSymbolStorage(); ns.AddProcedure(proc); ns.AddDefinedType(fptr); NativeDefinedType temp = null; NativeFunctionPointer retPtr = null; Assert.True(ns.TryGetGlobalSymbol(fptr.Name, out temp)); retPtr = (NativeFunctionPointer)temp; Assert.Equal(NativeCallingConvention.Pascal, retPtr.CallingConvention); NativeProcedure retProc = null; Assert.True(ns.TryGetGlobalSymbol(proc.Name, out retProc)); Assert.Equal(NativeCallingConvention.CDeclaration, retProc.CallingConvention); }
/// <summary> /// Verification of the generated code /// </summary> /// <param name="ns"></param> /// <remarks></remarks> private static void VerifyGeneratedStorage(BasicSymbolStorage ns) { NativeProcedure proc = null; VerifyTrue(ns.TryGetGlobalSymbol("SendMessageA", out proc)); VerifyTrue(ns.TryGetGlobalSymbol("SendMessageW", out proc)); VerifyTrue(ns.TryGetGlobalSymbol("GetForegroundWindow", out proc)); VerifyTrue(ns.TryGetGlobalSymbol("CreateWellKnownSid", out proc)); NativeTypeDef typedef = null; VerifyTrue(ns.TryGetGlobalSymbol("LPCSTR", out typedef)); VerifyTrue(ns.TryGetGlobalSymbol("LPWSTR", out typedef)); NativeType defined = null; VerifyTrue(ns.TryGetType("WNDPROC", out defined)); VerifyTrue(ns.TryGetType("HOOKPROC", out defined)); VerifyTrue(ns.TryGetType("tagPOINT", out defined)); VerifyTrue(ns.TryGetType("_SYSTEM_INFO", out defined)); NativeConstant c = null; VerifyTrue(ns.TryGetGlobalSymbol("WM_PAINT", out c)); VerifyTrue(ns.TryGetGlobalSymbol("WM_LBUTTONDOWN", out c)); }
private void TestRoundTrip(NativeSymbol symbol) { var storage = new BasicSymbolStorage(); var name = NativeNameUtil.GetName(symbol); var globalSymbol = new NativeGlobalSymbol(name, symbol); storage.Add(globalSymbol); TestRoundTrip(storage); }
private static BasicSymbolStorage Generate(TextWriter writer) { NativeCodeAnalyzer analyzer = NativeCodeAnalyzerFactory.Create(OsVersion.WindowsVista); analyzer.IncludePathList.AddRange(NativeCodeAnalyzerFactory.GetCommonSdkPaths()); // Run the preprocessor analyzer.Trace = true; string winPath = Path.Combine(PInvoke.Parser.NativeCodeAnalyzerFactory.GetPlatformSdkIncludePath(), "windows.h"); TextReaderBag tr = analyzer.RunPreProcessor(winPath); File.WriteAllText("d:\\temp\\windows.out.h", tr.TextReader.ReadToEnd()); analyzer.Trace = false; NativeCodeAnalyzerResult result = analyzer.Analyze(winPath); ErrorProvider ep = result.ErrorProvider; if (ep.Errors.Count > 0) { Debug.Fail("Encountered an error during the parse"); } NativeSymbolBag bag = NativeSymbolBag.CreateFrom(result, CreateInitialBasicSymbolStorage(), ep); // Resolve with the full dll list using (ProcedureFinder finder = new ProcedureFinder(FullDllList)) { bag.TryResolveSymbolsAndValues(finder, ep); } foreach (string msg in ep.AllMessages) { writer.WriteLine("' " + msg); } // GenerateCode(writer, bag) // Now write out the file var ns = new BasicSymbolStorage(); bag.SaveToNativeStorage(ns); VerifyGeneratedStorage(ns); // TODO: need to write to file again otherwise it's just in memory. // Copy the file to the various applications File.Copy("windows.xml", "..\\..\\..\\ConsoleTool\\bin\\Debug\\windows.xml", true); File.Copy("windows.xml", "..\\..\\Data\\windows.xml", true); string fullInstallTarget = Path.Combine(Environment.GetEnvironmentVariable("ProgramFiles"), Path.Combine(PInvoke.Constants.ProductName, "Data\\windows.xml")); if (File.Exists(fullInstallTarget)) { File.Copy("windows.xml", fullInstallTarget, true); } return(ns); }
public void EnumWithValues() { var e = new NativeEnum("e1"); e.Values.Add(new NativeEnumValue("e1", "v1")); e.Values.Add(new NativeEnumValue("e1", "v2")); var storage = new BasicSymbolStorage(); storage.AddEnumAndValues(e); TestRoundTrip(storage); }
public void ValueFromStorage1() { var ns = new BasicSymbolStorage(); ns.AddConstant(new NativeConstant("c1", "1")); var bag = new NativeSymbolBag(ns); bag.AddConstant(new NativeConstant("c2", "5+c1")); Assert.Equal(1, bag.FindUnresolvedNativeValues().Count); Assert.True(bag.TryResolveSymbolsAndValues()); }
private BasicSymbolStorage Go() { var storage = new BasicSymbolStorage(); while (!_reader.IsDone()) { var symbol = Import(); storage.Add(symbol); } return(storage); }
public void FindOrLoad1() { var ns = new BasicSymbolStorage(); ns.AddDefinedType(new NativeStruct("s1")); NativeSymbolBag bag = new NativeSymbolBag(ns); NativeDefinedType s1 = null; Assert.False(bag.Storage.TryGetGlobalSymbol("s1", out s1)); Assert.True(bag.TryGetGlobalSymbol("s1", out s1)); Assert.True(bag.TryGetGlobalSymbol("s1", out s1)); }
public void FindOrLoad3() { var ns = new BasicSymbolStorage(); ns.AddConstant(new NativeConstant("c1", "value")); NativeSymbolBag bag = new NativeSymbolBag(ns); NativeConstant c = null; Assert.False(bag.Storage.TryGetGlobalSymbol("c1", out c)); Assert.True(bag.TryGetGlobalSymbol("c1", out c)); Assert.True(bag.TryGetGlobalSymbol("c1", out c)); }
public void FindOrLoad2() { var ns = new BasicSymbolStorage(); ns.AddTypeDef(new NativeTypeDef("td", new NativeBuiltinType(BuiltinType.NativeChar))); NativeSymbolBag bag = new NativeSymbolBag(ns); NativeTypeDef td = null; Assert.False(bag.Storage.TryGetGlobalSymbol("td", out td)); Assert.True(bag.TryGetGlobalSymbol("td", out td)); Assert.True(bag.TryGetGlobalSymbol("td", out td)); }
public void ValueFromStorage3() { var ns = new BasicSymbolStorage(); NativeSymbolBag bag = new NativeSymbolBag(ns); ns.AddDefinedType(new NativeStruct("s1")); NativeConstant ntConst1 = new NativeConstant("c1", "(s1)1"); bag.AddConstant(ntConst1); Assert.Equal(1, bag.FindUnresolvedNativeValues().Count); Assert.True(bag.TryResolveSymbolsAndValues()); }
public void LoadByName1() { NativeStruct s1 = new NativeStruct("s"); s1.Members.Add(new NativeMember("m1", new NativeBuiltinType(BuiltinType.NativeInt32))); NativeType s2 = null; var ns = new BasicSymbolStorage(); ns.AddDefinedType(s1); Assert.True(ns.TryGetGlobalSymbol(s1.Name, out s2)); }
public void FindOrLoad4() { var ns = new BasicSymbolStorage(); NativeProcedure p1 = new NativeProcedure("p1"); p1.Signature.ReturnType = new NativeBuiltinType(BuiltinType.NativeBoolean); ns.AddProcedure(p1); NativeSymbolBag bag = new NativeSymbolBag(ns); NativeProcedure p = null; Assert.False(bag.Storage.TryGetGlobalSymbol("p1", out p)); Assert.True(bag.TryGetGlobalSymbol("p1", out p)); Assert.True(bag.TryGetGlobalSymbol("p1", out p)); }
public void Proc1() { NativeProcedure p1 = new NativeProcedure("p1"); p1.Signature.ReturnType = new NativeBuiltinType(BuiltinType.NativeByte); var ns = new BasicSymbolStorage(); ns.AddProcedure(p1); NativeProcedure retp1 = null; Assert.True(ns.TryGetGlobalSymbol(p1.Name, out retp1)); Assert.Equal(p1.DisplayName, retp1.DisplayName); }
public void ResolveLoad1() { var ns = new BasicSymbolStorage(); ns.AddDefinedType(new NativeStruct("s1")); NativeSymbolBag bag = new NativeSymbolBag(ns); bag.AddTypeDef(new NativeTypeDef("S1", "s1")); Assert.True(bag.TryResolveSymbolsAndValues()); NativeDefinedType td = null; Assert.True(bag.TryGetGlobalSymbol("s1", out td)); }
public void SaveAndLoad3() { NativeStruct s1 = new NativeStruct("s1"); s1.Members.Add(new NativeMember("m1", new NativeNamedType("foo"))); var ns = new BasicSymbolStorage(); ns.AddDefinedType(s1); NativeType rets1 = null; Assert.True(ns.TryGetType(s1.Name, out rets1)); Assert.True(NativeTypeEqualityComparer.AreEqualTopLevel(s1, rets1)); }
public void SaveAndLoad4() { NativeTypeDef t1 = new NativeTypeDef("t1"); t1.RealType = new NativeBuiltinType(BuiltinType.NativeByte); var ns = new BasicSymbolStorage(); ns.AddTypeDef(t1); NativeType rett1 = null; Assert.True(ns.TryGetType(t1.Name, out rett1)); Assert.True(NativeTypeEqualityComparer.AreEqualRecursive(rett1, t1)); }
public void Sal2() { NativeProcedure p1 = new NativeProcedure("p1"); p1.Signature.ReturnType = new NativeBuiltinType(BuiltinType.NativeChar); p1.Signature.ReturnTypeSalAttribute = new NativeSalAttribute(new NativeSalEntry(SalEntryType.Deref, "foo")); var ns = new BasicSymbolStorage(); ns.AddProcedure(p1); NativeProcedure retp1 = null; Assert.True(ns.TryGetGlobalSymbol(p1.Name, out retp1)); Assert.Equal("Deref(foo)", retp1.Signature.ReturnTypeSalAttribute.DisplayName); }
public void FuncPtr1() { NativeFunctionPointer fptr = new NativeFunctionPointer("f1"); fptr.Signature.ReturnType = new NativeBuiltinType(BuiltinType.NativeChar); fptr.Signature.Parameters.Add(new NativeParameter("f", new NativeBuiltinType(BuiltinType.NativeFloat))); var ns = new BasicSymbolStorage(); ns.AddDefinedType(fptr); NativeDefinedType retFptr = null; Assert.True(ns.TryGetGlobalSymbol(fptr.Name, out retFptr)); Assert.Equal("char (*f1)(float f)", ((NativeFunctionPointer)retFptr).DisplayName); }
internal static void Main(string[] args) { var dataDirectory = @"..\..\..\StorageGenerator\Data"; var oldFilePath = Path.Combine(dataDirectory, "windows.xml"); var newFilePath = Path.Combine(dataDirectory, "windows.csv"); Console.WriteLine("Reading old data"); var nativeStorage = new NativeStorage(); nativeStorage.ReadXml(oldFilePath); Console.WriteLine($"Converting to a {nameof(BasicSymbolStorage)}"); var storage = new BasicSymbolStorage(); foreach (var name in nativeStorage.NativeNames) { if (name.Kind == NativeNameKind.EnumValue) { continue; } NativeGlobalSymbol symbol; if (!nativeStorage.TryGetGlobalSymbol(name, out symbol)) { Console.WriteLine($"Error: Unable to load {name.Name} {name.Kind}"); continue; } storage.Add(symbol); } Console.WriteLine("Saving new data"); using (var stream = File.Open(newFilePath, FileMode.Create)) { StorageUtil.WriteCsv(stream, storage); } Console.WriteLine("Loading data for sanity check"); using (var stream = File.Open(newFilePath, FileMode.Open)) { var testStorage = StorageUtil.ReadCsv(stream); if (testStorage.Count != storage.Count) { Console.WriteLine("Error: Different count on load"); } } }
public void ValueFromStorage2() { var ns = new BasicSymbolStorage(); var bag = new NativeSymbolBag(ns); NativeEnum ntEnum = new NativeEnum("e1"); ntEnum.AddValue("v1", "5"); bag.AddDefinedType(ntEnum); NativeConstant ntConst1 = new NativeConstant("c1", "5+v1"); bag.AddConstant(ntConst1); Assert.Equal(1, bag.FindUnresolvedNativeValues().Count); Assert.True(bag.TryResolveSymbolsAndValues()); }
private static BasicSymbolStorage CreateInitialBasicSymbolStorage() { var ns = new BasicSymbolStorage(); // Add in the basic type defs ns.AddTypeDef(new NativeTypeDef("SIZE_T", new NativeBuiltinType(BuiltinType.NativeInt32, true))); ns.AddTypeDef(new NativeTypeDef("DWORD64", new NativeBuiltinType(BuiltinType.NativeInt64, true))); ns.AddTypeDef(new NativeTypeDef("HWND", new NativePointer(BuiltinType.NativeVoid))); ns.AddTypeDef(new NativeTypeDef("HMENU", new NativePointer(BuiltinType.NativeVoid))); ns.AddTypeDef(new NativeTypeDef("HACCEL", new NativePointer(BuiltinType.NativeVoid))); ns.AddTypeDef(new NativeTypeDef("HBRUSH", new NativePointer(BuiltinType.NativeVoid))); ns.AddTypeDef(new NativeTypeDef("HFONT", new NativePointer(BuiltinType.NativeVoid))); ns.AddTypeDef(new NativeTypeDef("HDC", new NativePointer(BuiltinType.NativeVoid))); ns.AddTypeDef(new NativeTypeDef("HICON", new NativePointer(BuiltinType.NativeVoid))); return(ns); }
public void ResolveLoad2() { var ns = new BasicSymbolStorage(); ns.AddTypeDef(new NativeTypeDef("TEST_INT", BuiltinType.NativeInt32)); NativeStruct s1 = new NativeStruct("s1"); s1.Members.Add(new NativeMember("m1", new NativeNamedType("TEST_INT"))); NativeSymbolBag bag = new NativeSymbolBag(ns); bag.AddDefinedType(s1); Assert.True(bag.TryResolveSymbolsAndValues()); NativeTypeDef td = null; Assert.True(bag.TryGetGlobalSymbol("TEST_INT", out td)); }
public void LoadByName2() { NativeStruct s1 = new NativeStruct("s"); s1.Members.Add(new NativeMember("m1", new NativeBuiltinType(BuiltinType.NativeInt32))); s1.Members.Add(new NativeMember("m2", new NativeBuiltinType(BuiltinType.NativeByte))); s1.Members.Add(new NativeMember("m3", new NativeBitVector(6))); s1.Members.Add(new NativeMember("m4", new NativePointer(new NativeBuiltinType(BuiltinType.NativeChar)))); s1.Members.Add(new NativeMember("m5", new NativeArray(new NativeBuiltinType(BuiltinType.NativeFloat), 4))); s1.Members.Add(new NativeMember("m7", new NativeNamedType("bar", new NativeBuiltinType(BuiltinType.NativeDouble)))); NativeType s2 = null; var ns = new BasicSymbolStorage(); ns.AddDefinedType(s1); Assert.True(ns.TryGetType(s1.Name, out s2)); }
public void SaveAndLoad1() { NativeStruct s1 = new NativeStruct("s1"); s1.Members.Add(new NativeMember("m1", new NativeBuiltinType(BuiltinType.NativeFloat))); NativeStruct s2 = new NativeStruct("s2"); s2.Members.Add(new NativeMember("m1", s1)); var ns = new BasicSymbolStorage(); ns.AddDefinedType(s2); NativeDefinedType rets2 = null; Assert.True(ns.TryGetGlobalSymbol(s2.Name, out rets2)); Assert.NotNull(rets2); Assert.True(NativeTypeEqualityComparer.AreEqualRecursive(s2, rets2)); Assert.True(NativeTypeEqualityComparer.AreEqualTopLevel(s2, rets2)); }
public void SaveAndLoad5() { NativeConstant c1 = new NativeConstant("c1", "v1"); NativeConstant c2 = new NativeConstant("c2", "v2", ConstantKind.MacroMethod); var ns = new BasicSymbolStorage(); ns.AddConstant(c1); ns.AddConstant(c2); NativeConstant ret = null; Assert.True(ns.TryGetGlobalSymbol("c1", out ret)); Assert.Equal("c1", ret.Name); Assert.Equal("v1", ret.Value.Expression); Assert.Equal(ConstantKind.Macro, ret.ConstantKind); Assert.True(ns.TryGetGlobalSymbol("c2", out ret)); Assert.Equal("c2", ret.Name); Assert.Equal("\"v2\"", ret.Value.Expression); Assert.Equal(ConstantKind.MacroMethod, ret.ConstantKind); }
public void Sal3() { NativeParameter param = new NativeParameter("p"); param.SalAttribute = new NativeSalAttribute(SalEntryType.Deref); param.NativeType = new NativeBuiltinType(BuiltinType.NativeChar); NativeProcedure p1 = new NativeProcedure("p1"); p1.Signature.ReturnType = new NativeBuiltinType(BuiltinType.NativeChar); p1.Signature.Parameters.Add(param); var ns = new BasicSymbolStorage(); ns.AddProcedure(p1); NativeProcedure retp1 = null; Assert.True(ns.TryGetGlobalSymbol(p1.Name, out retp1)); Assert.Equal("Deref", retp1.Signature.Parameters[0].SalAttribute.DisplayName); }
public void Proc4() { NativeStruct s1 = new NativeStruct("s1"); s1.Members.Add(new NativeMember("m1", new NativeBuiltinType(BuiltinType.NativeByte))); NativeProcedure p1 = new NativeProcedure("p1"); p1.Signature.ReturnType = s1; var ns = new BasicSymbolStorage(); ns.AddProcedure(p1); NativeProcedure retp1 = null; Assert.True(ns.TryGetGlobalSymbol(p1.Name, out retp1)); Assert.Equal(p1.DisplayName, retp1.DisplayName); NativeDefinedType rets1 = null; Assert.False(ns.TryGetGlobalSymbol(s1.Name, out rets1)); }
/// <summary> /// Used to create a simple set of types that can be used for testing purposes /// </summary> /// <returns></returns> /// <remarks></remarks> public static BasicSymbolStorage CreateStandard() { var storage = new BasicSymbolStorage(); NativePointer pt1 = default(NativePointer); NativeTypeDef td1 = default(NativeTypeDef); NativeTypeDef td2 = default(NativeTypeDef); NativeStruct s1 = default(NativeStruct); NativeUnion u1 = default(NativeUnion); NativeNamedType n1 = default(NativeNamedType); // Include sal information List <NativeConstant> list = ProcessSal(); foreach (NativeConstant cur in list) { storage.AddConstant(cur); } // Bool types storage.AddTypeDef(new NativeTypeDef("BOOL", BuiltinType.NativeInt32)); storage.AddTypeDef(new NativeTypeDef("DWORD", new NativeBuiltinType(BuiltinType.NativeInt32, true))); // WPARAM td1 = new NativeTypeDef("UINT_PTR", new NativeBuiltinType(BuiltinType.NativeInt32, true)); storage.AddTypeDef(new NativeTypeDef("WPARAM", td1)); storage.AddTypeDef(new NativeTypeDef("LPARAM", td1)); // WCHAR NativeTypeDef wcharTd = new NativeTypeDef("WCHAR", new NativeBuiltinType(BuiltinType.NativeInt16, true)); storage.AddTypeDef(wcharTd); // CHAR td1 = new NativeTypeDef("CHAR", BuiltinType.NativeChar); storage.AddTypeDef(td1); // TCHAR td2 = new NativeTypeDef("TCHAR", td1); storage.AddTypeDef(td2); // LPWSTR pt1 = new NativePointer(wcharTd); td2 = new NativeTypeDef("LPWSTR", pt1); storage.AddTypeDef(td2); // LPCWSTR n1 = new NativeNamedType(wcharTd.Name, wcharTd); n1.IsConst = true; pt1 = new NativePointer(n1); td2 = new NativeTypeDef("LPCWSTR", pt1); storage.AddTypeDef(td2); // LPSTR pt1 = new NativePointer(new NativeBuiltinType(BuiltinType.NativeChar)); td1 = new NativeTypeDef("LPSTR", pt1); storage.AddTypeDef(td1); // LPTSTR storage.AddTypeDef(new NativeTypeDef("LPTSTR", td1)); // LPCSTR n1 = new NativeNamedType("char", true); n1.RealType = new NativeBuiltinType(BuiltinType.NativeChar, false); pt1 = new NativePointer(n1); td1 = new NativeTypeDef("LPCSTR", pt1); storage.AddTypeDef(td1); // LPCTSTR td2 = new NativeTypeDef("LPCTSTR", td1); storage.AddTypeDef(td2); // BSTR storage.AddTypeDef(new NativeTypeDef("OLECHAR", BuiltinType.NativeWChar)); storage.AddTypeDef(new NativeTypeDef("BSTR", new NativePointer(new NativeTypeDef("OLECHAR", BuiltinType.NativeWChar)))); // Struct with a recrsive reference to itself s1 = new NativeStruct("RecursiveStruct"); s1.Members.Add(new NativeMember("m1", new NativePointer(new NativeNamedType(s1.Name)))); storage.AddDefinedType(s1); // Simple struct s1 = new NativeStruct("S1"); s1.Members.Add(new NativeMember("m1", new NativeBuiltinType(BuiltinType.NativeBoolean))); storage.AddDefinedType(s1); // Simulate a few well known structures // DECIMAL s1 = new NativeStruct("tagDEC"); storage.AddDefinedType(s1); storage.AddTypeDef(new NativeTypeDef("DECIMAL", s1)); // CURRENCY u1 = new NativeUnion("tagCY"); storage.AddDefinedType(u1); storage.AddTypeDef(new NativeTypeDef("CY", u1)); storage.AddTypeDef(new NativeTypeDef("CURRENCY", new NativeTypeDef("CY", u1))); // BYTE storage.AddTypeDef(new NativeTypeDef("BYTE", new NativeBuiltinType(BuiltinType.NativeChar, true))); return(storage); }