protected void CallFunction(params object[] parameters) { StackTrace st = new StackTrace(); StackFrame frame = st.GetFrame(1); var method = frame.GetMethod(); var attribute = method.GetCustomAttributes(typeof(ExternFunctionAttribute), false); ExternFunctionAttribute externAtt = null; if (attribute.Length > 0) { externAtt = (ExternFunctionAttribute)attribute[0]; } XmlDocument doc = PrepareDocument(externAtt, parameters); string s = GetXmlDocumentAsString(doc); string pipe = "extern_" + process.Id.ToString(); NamedPipeClientStream client = new NamedPipeClientStream(".", pipe, PipeDirection.InOut, PipeOptions.Asynchronous); client.Connect(); client.Write(Encoding.ASCII.GetBytes(s), 0, s.Length); client.Close(); }
private XmlDocument PrepareDocument(ExternFunctionAttribute att, params object[] parameters) { XmlDocument doc = new XmlDocument(); XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "UTF-8", ""); doc.AppendChild(dec); XmlElement exter = doc.CreateElement("Extern"); XmlElement element = doc.CreateElement("Address"); if (att.UseStaticAddress) { element.InnerText = att.Address.ToString(); } else { element.InnerText = GetAddress(att.Module, att.Offset).ToString(); } exter.AppendChild(element); element = doc.CreateElement("CallingConvention"); element.InnerText = ((int)att.CallingConvention).ToString(); exter.AppendChild(element); element = doc.CreateElement("Parameters"); XmlAttribute count = doc.CreateAttribute("Count"); count.Value = parameters.Length.ToString(); element.Attributes.Append(count); XmlElement param = null; for (int i = parameters.Length - 1; i >= 0; i--) { param = doc.CreateElement("Parameter"); XmlAttribute attr = doc.CreateAttribute("Offset"); attr.Value = i.ToString(); param.Attributes.Append(attr); attr = doc.CreateAttribute("Type"); attr.Value = MapToUnmanagedType(parameters[i].GetType()); param.Attributes.Append(attr); //if (attr.Value == "struct") //{ // param.InnerText = Convert.ToBase64String(StructToByteArray(parameters[i])); //} //else if (attr.Value == "pointer") //{ // attr.Value = "struct"; // Pointer p = (Pointer)parameters[i]; // param.InnerText = Convert.ToBase64String(StructToByteArray(p.Value)); //} //else //{ param.InnerText = parameters[i].ToString(); // } element.AppendChild(param); } exter.AppendChild(element); doc.AppendChild(exter); return(doc); }