I am working on a project that needs to be able to call functions in Win32 DLLs. However, the name of the DLL, function, and data types of all arguments and return type are not known at compile time, so using DLLImport is not an option. Basically, this routine could feasibly call any function in any DLL and pass in any arguments. After a lot of searching, I have been able to successfully put together some code that can do this, including passing numerics and strings into the function and even passing numeric arguments by reference. However, I have hit a brick wall trying to pass strings back from the DLL.
To simplify testing, I used Visual Studio 6 to compile a simple Win32 DLL called PassCharPtr.dll containing one function as follows:
PassCharPtr.def file:
EXPORTS
TestPassCharPtr
PassCharPtr.h file:
#include <windows.h>
extern "C" int __stdcall TestPassCharPtr(LPSTR, LONG);
PassCharPtr.cpp file:
#include "PassCharPtr.h"
int WINAPI DllEntryPoint(HINSTANCE hinst,
unsigned long reason,
void*)
{
return 1;
}
/*----------------------------------------------------------------*/
int __stdcall TestPassCharPtr(LPSTR szString, LONG cSize)
{
MessageBox(0, szString, "Inside PassCharPtr.dll", 0);
char buffer[] = "abcdefghijklmnopqrstuvwxyz";
if (cSize > strlen(buffer))
{
strcpy(szString,buffer);
return strlen(buffer);
}
return -cSize;
}
To test my DLL, I created a simple VB6 application:
Private Declare Function TestPassCharPtr Lib "PassCharPtr" (ByVal buffer As String, ByVal lSize As Long) As Long
Private Sub btnTest_Click()
Dim sBuffer As String
Dim lResult As Long
sBuffer = "This is a very long string!!!!!!!!!"
lResult = TestPassCharPtr(sBuffer, Len(sBuffer))
Debug.Print "Result: "; lResult
Debug.Print "Buffer: "; Left(sBuffer, lResult)
End Sub
Everything is working great. Now, here's my C# test project in VS2010 that attempts to access this function:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.InteropServices;
namespace TestPassCharPtr
{
class Program
{
/// <summary>
/// Define DLL and function to call. Setup data types for return value and arguments
/// and setup values to pass into function.
///
/// All data types should be declared using C\C++ names to facilitate using
/// existing Win32 API documentation to define function calls.
///
/// When passing a string to a function call, enclose the string in quotes ("")
///
/// </summary>
/// <param name="args">Unused</param>
static void Main(string[] args)
{
string fileName = "PassCharPtr.dll";
string funcName = "TestPassCharPtr";
string returnType = "int";
// comma-delimited list of argument data types
// using this declaration successfully passes string in
// but generates exception when passing string back!
string argTypesList = "char[], int";
// using this declaration fails to pass string in, but does
// not generate an exception when passing string back!
//string argTypesList = "LPSTR, int";
// comma-delimited list of argument values
string argValuesList = "\"This is a very long string!!!!\", 30";
TestDLLFunction(fileName, funcName, returnType, argTypesList, argValuesList);
MessageBox.Show("Done");
}
/// <summary>
/// Calls a DLL function.
///
/// Reference: http://www.pcreview.co.uk/forums/calling-native-c-function-using-definepinvokemethod-and-returning-calculated-value-pointer-reference-t2329473.html
///
/// </summary>
/// <param name="dllFilename">Filename of DLL (excluding path!)</param>
/// <param name="entryPoint">Function name</param>
/// <param name="retType">Return value data type</param>
/// <param name="argTypesList">Comma-delimited list of argument data types</param>
/// <param name="argValuesList">Comma-delimited list of argument values</param>
private static void TestDLLFunction(string dllPath, string entryPoint, string retType, string argTypesList, string argValuesList)
{
Type returnType = null;
Type[] argTypesArray = null;
object[] argValuesArray = null;
object returnValue = null;
// get return data type
returnType = Type.GetType(ConvertToNetType(retType));
// get argument data types for the function to call
string[] argTypes = argTypesList.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
if (argTypes.Length > 0)
{
// create a list of data types for each argument
List<Type> listArgTypes = new List<Type>();
foreach (var argType in argTypes)
{
string netType = ConvertToNetType(argType);
string byRef = IsPointer(argType) ? "&" : "";
listArgTypes.Add(Type.GetType(netType + byRef));
}
// convert the list to an array
argTypesArray = listArgTypes.ToArray<Type>();
// get values to pass as arguments to the function
string[] argValues = argValuesList.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
// remove quotes from strings
for (int i = 0; i < argValues.Length; i++)
{
argValues[i] = argValues[i].Replace("\"", "").Trim();
}
argValuesArray = argValues.ToArray<object>();
// verify argument data types count and argument values count match!
if (argValuesArray.Length != argTypesArray.Length)
{
Console.WriteLine(string.Format("The number of parameter types ({0}) does not match the number of parameter values ({1}).", argTypesArray.Length, argValuesArray.Length));
return;
}
// convert all argument values to the proper data types
for (int i = 0; i < argValuesArray.Length; i++)
{
if (argTypesArray[i] == Type.GetType("System.IntPtr&"))
{
argValuesArray[i] = (IntPtr)0;
}
else
{
argValuesArray[i] = ConvertParameter(argValuesArray[i], argTypesArray[i]);
}
}
}
else
{
argTypesArray = null;
argValuesArray = null;
}
// Create a dynamic assembly and a dynamic module
AssemblyName assemblyName = new AssemblyName();
assemblyName.Name = dllPath;
AssemblyBuilder dynamicAssembly = AppDomain.CurrentDomain.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run);
ModuleBuilder dynamicModule = dynamicAssembly.DefineDynamicModule("tempModule");
// Dynamically construct a global PInvoke signature using the input information
MethodBuilder dynamicMethod = dynamicModule.DefinePInvokeMethod(entryPoint, dllPath,
MethodAttributes.Static | MethodAttributes.Public | MethodAttributes.PinvokeImpl,
CallingConventions.Standard, returnType, argTypesArray, CallingConvention.Winapi, CharSet.Ansi);
// Add PreserveSig to the method implementation flags. NOTE: If this line
// is commented out, the return value will be zero when the method is invoked.
dynamicMethod.SetImplementationFlags(dynamicMethod.GetMethodImplementationFlags() | MethodImplAttributes.PreserveSig);
// This global method is now complete
dynamicModule.CreateGlobalFunctions();
// Get a MethodInfo for the PInvoke method
MethodInfo mi = dynamicModule.GetMethod(entryPoint);
// Invoke the function
try
{
returnValue = mi.Invoke(null, argValuesArray);
}
catch (Exception ex)
{
Console.WriteLine(string.Format("Error: {0}", ex.Message));
if (ex.InnerException != null)
{
Console.WriteLine(string.Format(" Error: {0}", ex.InnerException.Message));
}
}
if (returnValue != null)
{
Console.WriteLine(string.Format("Return value: {0}", returnValue.ToString()));
}
if (argValuesArray != null)
{
for (int i = 0; i < argValuesArray.Length; i++)
{
if (argValuesArray[i] != null)
{
Console.WriteLine(string.Format("Argument {0}: {1}", i, argValuesArray[i].ToString()));
}
}
}
}
/// <summary>
/// Converts a string to another data type.
/// </summary>
/// <param name="value">Value to be converted</param>
/// <param name="dataType">Data type to convert to</param>
/// <returns>Converted value</returns>
private static object ConvertParameter(object value, Type dataType)
{
// determine the base data type (remove "&" from end of "by reference" data types)
string baseDataType = dataType.ToString();
if (baseDataType.EndsWith("&"))
{
baseDataType = baseDataType.Substring(0, baseDataType.Length - 1);
}
return Convert.ChangeType(value, Type.GetType(baseDataType));
}
/// <summary>
/// Determines whether the indicated native data type is a pointer
/// </summary>
/// <param name="dataType">Native (unmanaged) data type</param>
/// <returns>true if data type is a pointer; false otherwise</returns>
private static bool IsPointer(string dataType)
{
string lowerDataType = dataType.ToLower();
if (lowerDataType.StartsWith("lp") || lowerDataType.EndsWith("*"))
{
return true;
}
return false;
}
/// <summary>
/// Convert unmanaged data type names to .NET data type names
///
/// (for simplicity, all types unused by this example were removed)
///
/// </summary>
/// <param name="type">Unmanaged data type name</param>
/// <returns>Corresponding .NET data type name</returns>
private static string ConvertToNetType(string type)
{
string lowerType = type.ToLower();
if (lowerType.Contains("int"))
{
return "System.Int32";
}
else if (lowerType.Contains("lpstr"))
{
return "System.IntPtr";
}
else if (lowerType.Contains("char[]"))
{
return "System.String";
}
return "";
}
}
}
If I declare the first argument as char[] (System.String), I can successfully pass a string into the function, but it generates an exception (accessing protected memory) when the DLL attempts to replace that string with the string to return.
If I declare the first argument as LPSTR (System.IntPtr), I cannot pass a string into the function. However, upon returning from the call, argValuesArray[0] contains what appears to be an address. I have not been able to figure out how to convert that address into the returned string yet. I have tried using String mvValue = Marshal.PtrToStringAnsi((IntPtr)argValuesArray[0]), but this returns an empty string.
There are still a lot of holes in this code, but I hope the general idea is clear enough. Can anyone tell me what data type the first argument should be declared as to be able to pass strings both into and out of this function successfully and how to do any necessary conversions on that data type?
LPSTR is generally marshalled as StringBuilder.
Related
I am trying to set MailMerge fields and let Word fill them for me, which isn't a Problem at all... What i am looking to do and can't figure out is the following, I want to set 2 MailMergeFields in 1 place and let Word sort it out for me.
In this case have a mergefield for PO_Box and Adress, if there is a PO_Box # use it, otherwise use the Standard Adress.
Example of the MailMerge what it would look like in Word:
{ IF { MERGEFIELD PO_Box } > "1" "{ MERGEFIELD PO_Box }" "{ MERGEFIELD Adress }" \* MERGEFORMAT }
Is there a way to make this happen thru some Word Interop Funktion?
Edit:
static void Main(string[] args)
{
object fileName = #"C:\test.docx";
string dataSource = #"C:\Test.csv";
Word.Selection wrdSelection;
Word.MailMerge wrdMailMerge;
Word.MailMergeFields wrdMergeFields;
// Start Word Application
Word.Application wrdApp = new Word.Application();
//Load a document
Word.Document wrdDoc = wrdApp.Documents.Add(ref fileName, Visible: true);
wrdSelection = wrdApp.Selection;
wrdMailMerge = wrdDoc.MailMerge;
// Open Data Source from .csv file
wrdDoc.MailMerge.OpenDataSource(dataSource);
//Create MergeFields
wrdSelection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphLeft;
wrdSelection.ParagraphFormat.LineSpacingRule = Word.WdLineSpacing.wdLineSpaceSingle;
wrdSelection.ParagraphFormat.SpaceAfter = 0.0F;
wrdMergeFields = wrdMailMerge.Fields;
wrdMergeFields.Add(wrdSelection.Range, "Title");
wrdSelection.TypeText(" ");
wrdMergeFields.Add(wrdSelection.Range, "FirstName");
wrdSelection.TypeText(" ");
wrdMergeFields.Add(wrdSelection.Range, "LastName");
wrdSelection.TypeParagraph();
// Here I want to combine this Field with a PO_Box and let Word
// do the trick
wrdMergeFields.Add(wrdSelection.Range, "Address");
wrdSelection.TypeParagraph();
wrdMergeFields.Add(wrdSelection.Range, "City");
wrdSelection.TypeText(", ");
wrdMergeFields.Add(wrdSelection.Range, "State");
wrdSelection.TypeText(" ");
wrdMergeFields.Add(wrdSelection.Range, "Zip");
wrdSelection.ParagraphFormat.LineSpacingRule = Word.WdLineSpacing.wdLineSpaceDouble;
insertLines(wrdApp, 2);
//Right justify the line and insert a date field with current date.
wrdSelection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight;
object objDate = "dd.MM.yyyy";
wrdSelection.InsertDateTime(ref objDate);
//Preview the final merge
wrdDoc.MailMerge.Destination = Word.WdMailMergeDestination.wdSendToNewDocument;
wrdDoc.MailMerge.Execute();
//Close Template
object saveOption = Word.WdSaveOptions.wdDoNotSaveChanges;
wrdDoc.Close(ref saveOption);
//Shows the Application after the process to the User
wrdApp.Visible = true;
}
public static void insertLines(Word.Application wrdApp, int LineNum)
{
int iCount;
// Insert "LineNum" blank lines.
for (iCount = 1; iCount <= LineNum; iCount++)
{
wrdApp.Selection.TypeParagraph();
}
}
So this basicly what I have, now i need the Adress MergeField to behave as i described above, since I will receive a .csv data from another programm that i can't modify I would like to place this field in Word that it will sort out if there is a PO Box or Adress.
So, what you really want is to create the nested field codes. There are two basic approaches for this:
Record a macro while doing it as a user as the basis. This relies on the Selection object, which can be tricky; the approach is not scalable (only works for that specific combination). This is described on StackOverflow, so I won't repeat it here: Setting up a nested field in Word using VBA
Insert the field as a string, using "placeholders" to indicate
where the field codes are, then convert the placeholders to field
codes. This is scalable: it can be used for any combination of
fields. There is an excellent algorithm in C# posted on GitHub, by Florian Wolters in response to a discussion in which I participated on MSDN. I copy it below for convenience.
https://gist.github.com/FlorianWolters/6257233
//------------------------------------------------------------------------------
// <copyright file="FieldCreator.cs" company="Florian Wolters">
// Copyright (c) Florian Wolters. All rights reserved.
// </copyright>
// <author>Florian Wolters <wolters.fl#gmail.com></author>
//------------------------------------------------------------------------------
namespace FlorianWolters.Office.Word.Fields
{
using System;
using System.Collections;
using System.Runtime.InteropServices;
using Word = Microsoft.Office.Interop.Word;
/// <summary>
/// The class <see cref="FieldCreator"/> simplifies the creation of <see cref="Word.Field"/>s.
/// </summary>
public class FieldCreator
{
/// <summary>
/// Adds one or more new <see cref="Word.Field"/> to the specified <see cref="Word.Range"/>.
/// <para>
/// This method allows to insert nested fields at the specified range.
/// </para>
/// <example>
/// <c>InsertField(Application.Selection.Range, {{= {{PAGE}} - 1}};</c>
/// will produce
/// { = { PAGE } - 1 }
/// </example>
/// </summary>
/// <param name="range">The <see cref="Word.Range"/> where to add the <see cref="Word.Field"/>.</param>
/// <param name="theString">The string to convert to one or more <see cref="Word.Field"/> objects.</param>
/// <param name="fieldOpen">The special code to mark the start of a <see cref="Word.Field"/>.</param>
/// <param name="fieldClose">The special code to mark the end of a <see cref="Word.Field"/>.</param>
/// <returns>The newly created <see cref="Word.Field"/></returns>
/// <remarks>
/// A solution for VBA has been taken from this
/// article and adopted for C# by the author.
/// </remarks>
public Word.Field InsertField(
Word.Range range,
string theString = "{{}}",
string fieldOpen = "{{",
string fieldClose = "}}")
{
if (null == range)
{
throw new ArgumentNullException("range");
}
if (string.IsNullOrEmpty(fieldOpen))
{
throw new ArgumentException("fieldOpen");
}
if (string.IsNullOrEmpty(fieldClose))
{
throw new ArgumentException("fieldClose");
}
if (!theString.Contains(fieldOpen) || !theString.Contains(fieldClose))
{
throw new ArgumentException("theString");
}
// Special case. If we do not check this, the algorithm breaks.
if (theString == fieldOpen + fieldClose)
{
return this.InsertEmpty(range);
}
// TODO Implement additional error handling.
// TODO Possible to remove the dependency to state capture?
using (new StateCapture(range.Application.ActiveDocument))
{
Word.Field result = null;
Stack fieldStack = new Stack();
range.Text = theString;
fieldStack.Push(range);
Word.Range searchRange = range.Duplicate;
Word.Range nextOpen = null;
Word.Range nextClose = null;
Word.Range fieldRange = null;
while (searchRange.Start != searchRange.End)
{
nextOpen = this.FindNextOpen(searchRange.Duplicate, fieldOpen);
nextClose = this.FindNextClose(searchRange.Duplicate, fieldClose);
if (null == nextClose)
{
break;
}
// See which marker comes first.
if (nextOpen.Start < nextClose.Start)
{
nextOpen.Text = string.Empty;
searchRange.Start = nextOpen.End;
// Field open, so push a new range to the stack.
fieldStack.Push(nextOpen.Duplicate);
}
else
{
nextClose.Text = string.Empty;
// Move start of main search region onwards past the end marker.
searchRange.Start = nextClose.End;
// Field close, so pop the last range from the stack and insert the field.
fieldRange = (Word.Range)fieldStack.Pop();
fieldRange.End = nextClose.End;
result = this.InsertEmpty(fieldRange);
}
}
// Move the current selection after all inserted fields.
// TODO Improvement possible, e.g. by using another range object?
int newPos = fieldRange.End + fieldRange.Fields.Count + 1;
fieldRange.SetRange(newPos, newPos);
fieldRange.Select();
// Update the result of the outer field object.
result.Update();
return result;
}
}
/// <summary>
/// Adds a new empty <see cref="Word.Field"/> to the specified <see cref="Word.Range"/>.
/// </summary>
/// <param name="range">The <see cref="Word.Range"/> where to add the <see cref="Word.Field"/>.</param>
/// <param name="preserveFormatting">
/// Whether to apply the formatting of the previous <see cref="Word.Field"/> result to the new result.
/// </param>
/// <returns>The newly created <see cref="Word.Field"/>.</returns>
public Word.Field InsertEmpty(Word.Range range, bool preserveFormatting = false)
{
Word.Field result = this.AddFieldToRange(range, Word.WdFieldType.wdFieldEmpty, preserveFormatting);
// Show the field codes of an empty field, because otherwise we can't be sure that it is visible.
result.ShowCodes = true;
return result;
}
/// <summary>
/// Creates a <see cref="Word.Field"/> and adds it to the specified <see cref="Word.Range"/>
/// </summary>
/// <remarks>
/// The <see cref="Word.Field"/> is added to the <see cref="Word.Fields"/> collection of the specified <see
/// cref="Word.Range"/>.
/// </remarks>
/// <param name="range">The <see cref="Word.Range"/> where to add the <see cref="Word.Field"/>.</param>
/// <param name="type">The type of <see cref="Word.Field"/> to create.</param>
/// <param name="preserveFormatting">
/// Whether to apply the formatting of the previous field result to the new result.
/// </param>
/// <param name="text">Additional text needed for the <see cref="Word.Field"/>.</param>
/// <returns>The newly created <see cref="Word.Field"/>.</returns>
private Word.Field AddFieldToRange(
Word.Range range,
Word.WdFieldType type,
bool preserveFormatting = false,
string text = null)
{
return range.Fields.Add(
range,
type,
(null == text) ? Type.Missing : text,
preserveFormatting);
}
private Word.Range FindNextOpen(Word.Range range, string text)
{
Word.Find find = this.CreateFind(range, text);
Word.Range result = range.Duplicate;
if (!find.Found)
{
// Make sure that the next closing field will be found first.
result.Collapse(Word.WdCollapseDirection.wdCollapseEnd);
}
return result;
}
private Word.Range FindNextClose(Word.Range range, string text)
{
return this.CreateFind(range, text).Found ? range.Duplicate : null;
}
private Word.Find CreateFind(Word.Range range, string text)
{
Word.Find result = range.Find;
result.Execute(FindText: text, Forward: true, Wrap: Word.WdFindWrap.wdFindStop);
return result;
}
}
}
I'm looking for a way to programmatically get the summary portion of Xml-comments of a method in ASP.net.
I have looked at the previous related posts and they do not supply a way of doing so in a web environment.
I can not use any 3rd party apps and due to a web environment, Visual studio plugin's aren't much use either.
The closest thing I have found to a working solution was the JimBlackler project, but it only works on DLL's.
Naturally, something like 'supply .CS file, get XML documentation' would be optimal.
Current situation
I have a web-service and trying to dynamically generate documentation for it.
Reading the Methods, and properties is easy, but getting the Summary for each method is throwing me off a bit.
/// <summary>
/// This Is what I'm trying to read
/// </summary>
public class SomeClass()
{
/// <summary>
/// This Is what I'm trying to read
/// </summary>
public void SomeMethod()
{
}
}
A Workaround - Using reflection on Program.DLL/EXE together with Program.XML file
If you take a look at the sibling .XML file generated by Visual Studio you will see that there is a fairly flat hierarchy of /members/member.
All you have to do is get hold on each method from your DLL via MethodInfo object. Once you have this object you turn to the XML and use XPATH to get the member containing the XML documentation for this method.
Members are preceded by a letter. XML doc for methods are preceded by "M:" for class by "T:" etc.
Load your sibling XML
string docuPath = dllPath.Substring(0, dllPath.LastIndexOf(".")) + ".XML";
if (File.Exists(docuPath))
{
_docuDoc = new XmlDocument();
_docuDoc.Load(docuPath);
}
Use this xpath to get the member representing the method XML docu
string path = "M:" + mi.DeclaringType.FullName + "." + mi.Name;
XmlNode xmlDocuOfMethod = _docuDoc.SelectSingleNode(
"//member[starts-with(#name, '" + path + "')]");
Now scan childnodes for all the rows of "///"
Sometimes the /// Summary contains extra blanks, if this bothers use this to remove
var cleanStr = Regex.Replace(row.InnerXml, #"\s+", " ");
The XML summary isn't stored in the .NET assembly - it's optionally written out to an XML file as part of your build (assuming you're using Visual Studio).
Consequently there is no way to "pull out" the XML summaries of each method via reflection on a compiled .NET assembly (either .EXE or .DLL) - because the data simply isn't there for you to pull out. If you want the data, you'll have to instruct your build environment to output the XML files as part of your build process and parse those XML files at runtime to get at the summary information.
You could 'document' your method using the System.ComponentModel.DataAnnotations.DisplayAttribute attribute, e.g.
[Display(Name = "Foo", Description = "Blah")]
void Foo()
{
}
then use reflection to pull the description at runtime.
A deleted post, made by #OleksandrIeremenko, on this thread links to this article https://jimblackler.net/blog/?p=49 which was the basis for my solution.
Below is a modification of Jim Blackler's code making extension methods off the MemberInfo and Type objects and adding code that returns the summary text or an empty string if not available.
Usage
var typeSummary = typeof([Type Name]).GetSummary();
var methodSummary = typeof([Type Name]).GetMethod("[Method Name]").GetSummary();
Extension Class
/// <summary>
/// Utility class to provide documentation for various types where available with the assembly
/// </summary>
public static class DocumentationExtensions
{
/// <summary>
/// Provides the documentation comments for a specific method
/// </summary>
/// <param name="methodInfo">The MethodInfo (reflection data ) of the member to find documentation for</param>
/// <returns>The XML fragment describing the method</returns>
public static XmlElement GetDocumentation(this MethodInfo methodInfo)
{
// Calculate the parameter string as this is in the member name in the XML
var parametersString = "";
foreach (var parameterInfo in methodInfo.GetParameters())
{
if (parametersString.Length > 0)
{
parametersString += ",";
}
parametersString += parameterInfo.ParameterType.FullName;
}
//AL: 15.04.2008 ==> BUG-FIX remove “()” if parametersString is empty
if (parametersString.Length > 0)
return XmlFromName(methodInfo.DeclaringType, 'M', methodInfo.Name + "(" + parametersString + ")");
else
return XmlFromName(methodInfo.DeclaringType, 'M', methodInfo.Name);
}
/// <summary>
/// Provides the documentation comments for a specific member
/// </summary>
/// <param name="memberInfo">The MemberInfo (reflection data) or the member to find documentation for</param>
/// <returns>The XML fragment describing the member</returns>
public static XmlElement GetDocumentation(this MemberInfo memberInfo)
{
// First character [0] of member type is prefix character in the name in the XML
return XmlFromName(memberInfo.DeclaringType, memberInfo.MemberType.ToString()[0], memberInfo.Name);
}
/// <summary>
/// Returns the Xml documenation summary comment for this member
/// </summary>
/// <param name="memberInfo"></param>
/// <returns></returns>
public static string GetSummary(this MemberInfo memberInfo)
{
var element = memberInfo.GetDocumentation();
var summaryElm = element?.SelectSingleNode("summary");
if (summaryElm == null) return "";
return summaryElm.InnerText.Trim();
}
/// <summary>
/// Provides the documentation comments for a specific type
/// </summary>
/// <param name="type">Type to find the documentation for</param>
/// <returns>The XML fragment that describes the type</returns>
public static XmlElement GetDocumentation(this Type type)
{
// Prefix in type names is T
return XmlFromName(type, 'T', "");
}
/// <summary>
/// Gets the summary portion of a type's documenation or returns an empty string if not available
/// </summary>
/// <param name="type"></param>
/// <returns></returns>
public static string GetSummary(this Type type)
{
var element = type.GetDocumentation();
var summaryElm = element?.SelectSingleNode("summary");
if (summaryElm == null) return "";
return summaryElm.InnerText.Trim();
}
/// <summary>
/// Obtains the XML Element that describes a reflection element by searching the
/// members for a member that has a name that describes the element.
/// </summary>
/// <param name="type">The type or parent type, used to fetch the assembly</param>
/// <param name="prefix">The prefix as seen in the name attribute in the documentation XML</param>
/// <param name="name">Where relevant, the full name qualifier for the element</param>
/// <returns>The member that has a name that describes the specified reflection element</returns>
private static XmlElement XmlFromName(this Type type, char prefix, string name)
{
string fullName;
if (string.IsNullOrEmpty(name))
fullName = prefix + ":" + type.FullName;
else
fullName = prefix + ":" + type.FullName + "." + name;
var xmlDocument = XmlFromAssembly(type.Assembly);
var matchedElement = xmlDocument["doc"]["members"].SelectSingleNode("member[#name='" + fullName + "']") as XmlElement;
return matchedElement;
}
/// <summary>
/// A cache used to remember Xml documentation for assemblies
/// </summary>
private static readonly Dictionary<Assembly, XmlDocument> Cache = new Dictionary<Assembly, XmlDocument>();
/// <summary>
/// A cache used to store failure exceptions for assembly lookups
/// </summary>
private static readonly Dictionary<Assembly, Exception> FailCache = new Dictionary<Assembly, Exception>();
/// <summary>
/// Obtains the documentation file for the specified assembly
/// </summary>
/// <param name="assembly">The assembly to find the XML document for</param>
/// <returns>The XML document</returns>
/// <remarks>This version uses a cache to preserve the assemblies, so that
/// the XML file is not loaded and parsed on every single lookup</remarks>
public static XmlDocument XmlFromAssembly(this Assembly assembly)
{
if (FailCache.ContainsKey(assembly))
{
throw FailCache[assembly];
}
try
{
if (!Cache.ContainsKey(assembly))
{
// load the docuemnt into the cache
Cache[assembly] = XmlFromAssemblyNonCached(assembly);
}
return Cache[assembly];
}
catch (Exception exception)
{
FailCache[assembly] = exception;
throw;
}
}
/// <summary>
/// Loads and parses the documentation file for the specified assembly
/// </summary>
/// <param name="assembly">The assembly to find the XML document for</param>
/// <returns>The XML document</returns>
private static XmlDocument XmlFromAssemblyNonCached(Assembly assembly)
{
var assemblyFilename = assembly.Location;
if (!string.IsNullOrEmpty(assemblyFilename))
{
StreamReader streamReader;
try
{
streamReader = new StreamReader(Path.ChangeExtension(assemblyFilename, ".xml"));
}
catch (FileNotFoundException exception)
{
throw new Exception("XML documentation not present (make sure it is turned on in project properties when building)", exception);
}
var xmlDocument = new XmlDocument();
xmlDocument.Load(streamReader);
return xmlDocument;
}
else
{
throw new Exception("Could not ascertain assembly filename", null);
}
}
}
You can use Namotion.Reflection NuGet package to get these information:
string summary = typeof(Foo).GetXmlDocsSummary();
You can look at https://github.com/NSwag/NSwag - source for nuget NSwag.CodeGeneration - it gets summary as well, usage
var generator = new WebApiAssemblyToSwaggerGenerator(settings);<br/>
var swaggerService = generator.GenerateForController("namespace.someController");<br/>
// string with comments <br/>
var swaggerJson = swaggerService.ToJson();
(try ILSPY decompiler against your dll, you check code and comments)
If you have access to the source code you're trying to get comments for, then you can use Roslyn compiler platform to do that. It basically gives you access to all the intermediary compiler metadata and you can do anything you want with it.
It's a bit more complicated than what other people are suggesting, but depending on what your needs are, might be an option.
It looks like this post has a code sample for something similar.
Summary
I have an ASP.NET 3.5 website and a mobile detection method inside a C# class file inside my app_code folder. I want to call this method which sets a cookie, then switch my master page file if it's a mobile device.
I'm using a method i got from the comment section down in this article: http://www.codeproject.com/Articles/34422/Detecting-a-mobile-browser-in-ASP-NET
This just seemed simpler than using the 51degrees method of detection since i didn't really need a high level of detection, and i didn't want to send them to a different URL, but rather just flip to a different masterpage, and the NuGet package which makes a nice easy install doesn't work for ASP.NET 3.5.
The problem i'm at currently is with calling the method.
Here's the Code
External app_code class
public static class fooBar // test method
{
public static bool ean()
{
return true;
}
}
public static class HttpRequestExt
{
#region Private Fields
// These regular expressions retrieved from http://detectmobilebrowser.com/ "Open source mobile phone detection".
private static Regex MobileBrowsers = new Regex(#"android|avantgo|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|midp|mmp|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)\\/|plucker|pocket|psp|symbian|treo|up\\.(browser|link)|vodafone|wap|windows (ce|phone)|xda|xiino", RegexOptions.IgnoreCase | RegexOptions.Multiline);
private static Regex MobileApps = new Regex(#"1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a wa|abac|ac(er|oo|s\\-)|ai(ko|rn)|al(av|ca|co)|amoi|an(ex|ny|yw)|aptu|ar(ch|go)|as(te|us)|attw|au(di|\\-m|r |s )|avan|be(ck|ll|nq)|bi(lb|rd)|bl(ac|az)|br(e|v)w|bumb|bw\\-(n|u)|c55\\/|capi|ccwa|cdm\\-|cell|chtm|cldc|cmd\\-|co(mp|nd)|craw|da(it|ll|ng)|dbte|dc\\-s|devi|dica|dmob|do(c|p)o|ds(12|\\-d)|el(49|ai)|em(l2|ul)|er(ic|k0)|esl8|ez([4-7]0|os|wa|ze)|fetc|fly(\\-|_)|g1 u|g560|gene|gf\\-5|g\\-mo|go(\\.w|od)|gr(ad|un)|haie|hcit|hd\\-(m|p|t)|hei\\-|hi(pt|ta)|hp( i|ip)|hs\\-c|ht(c(\\-| |_|a|g|p|s|t)|tp)|hu(aw|tc)|i\\-(20|go|ma)|i230|iac( |\\-|\\/)|ibro|idea|ig01|ikom|im1k|inno|ipaq|iris|ja(t|v)a|jbro|jemu|jigs|kddi|keji|kgt( |\\/)|klon|kpt |kwc\\-|kyo(c|k)|le(no|xi)|lg( g|\\/(k|l|u)|50|54|e\\-|e\\/|\\-[a-w])|libw|lynx|m1\\-w|m3ga|m50\\/|ma(te|ui|xo)|mc(01|21|ca)|m\\-cr|me(di|rc|ri)|mi(o8|oa|ts)|mmef|mo(01|02|bi|de|do|t(\\-| |o|v)|zz)|mt(50|p1|v )|mwbp|mywa|n10[0-2]|n20[2-3]|n30(0|2)|n50(0|2|5)|n7(0(0|1)|10)|ne((c|m)\\-|on|tf|wf|wg|wt)|nok(6|i)|nzph|o2im|op(ti|wv)|oran|owg1|p800|pan(a|d|t)|pdxg|pg(13|\\-([1-8]|c))|phil|pire|pl(ay|uc)|pn\\-2|po(ck|rt|se)|prox|psio|pt\\-g|qa\\-a|qc(07|12|21|32|60|\\-[2-7]|i\\-)|qtek|r380|r600|raks|rim9|ro(ve|zo)|s55\\/|sa(ge|ma|mm|ms|ny|va)|sc(01|h\\-|oo|p\\-)|sdk\\/|se(c(\\-|0|1)|47|mc|nd|ri)|sgh\\-|shar|sie(\\-|m)|sk\\-0|sl(45|id)|sm(al|ar|b3|it|t5)|so(ft|ny)|sp(01|h\\-|v\\-|v )|sy(01|mb)|t2(18|50)|t6(00|10|18)|ta(gt|lk)|tcl\\-|tdg\\-|tel(i|m)|tim\\-|t\\-mo|to(pl|sh)|ts(70|m\\-|m3|m5)|tx\\-9|up(\\.b|g1|si)|utst|v400|v750|veri|vi(rg|te)|vk(40|5[0-3]|\\-v)|vm40|voda|vulc|vx(52|53|60|61|70|80|81|83|85|98)|w3c(\\-| )|webc|whit|wi(g |nc|nw)|wmlb|wonu|x700|xda(\\-|2|g)|yas\\-|your|zeto|zte\\-", RegexOptions.IgnoreCase | RegexOptions.Multiline);
#endregion
public const string ViewMobileSiteCookieName = "ViewMobile";
/// <summary>
/// Determines if the request emanated from a mobile-device client;
/// and stores the result in a cookie on the response.
/// </summary>
/// <param name="request"></param>
/// <param name="Response"></param>
/// <returns></returns>
///
public static bool IsMobileClient(this System.Web.HttpRequest request, System.Web.HttpRequest Response)
{
bool isMobile = false;
bool isCookieSet = false;
var viewMobileCookie = request.Cookies[ViewMobileSiteCookieName];
if (viewMobileCookie != null && bool.TryParse(viewMobileCookie.Value, out isMobile))
{
isCookieSet = true;
}
else if (request.Browser.IsMobileDevice)
{
isMobile = true;
}
else if (request.ServerVariables["HTTP_X_WAP_PROFILE"].IsNotEmpty())
{
isMobile = true;
}
else if
(
request.ServerVariables["HTTP_ACCEPT"].IsNotEmpty()
&&
(
request.ServerVariables["HTTP_ACCEPT"].ToLower().Contains("wap")
|| request.ServerVariables["HTTP_ACCEPT"].ToLower().Contains("wml+xml")
)
)
{
isMobile = true;
}
else if (request.ServerVariables["HTTP_USER_AGENT"].IsNotEmpty())
{
string userAgent = request.ServerVariables["HTTP_USER_AGENT"];
isMobile = ((MobileBrowsers.IsMatch(userAgent) || MobileApps.IsMatch(userAgent.Substring(0, 4))));
}
// Store the result as a cookie.
if (!isCookieSet)
Response.Cookies.Add(new HttpCookie(ViewMobileSiteCookieName, isMobile.ToString()));
return isMobile;
}
public static bool IsNotEmpty(this string instance)
{
return instance != null && instance.Length > 0;
}
}
My call to it)
Right now im doing it on the page, but i figure i'll do this in global.asax on session start?
sectionTitle.InnerHtml = fooBar.ean().ToString(); // test works
sectionTitle.InnerHtml = HttpRequestExt.IsMobileClient.ToString(); // compile error
Compile Error:
CS0119: 'SWIC.HttpRequestExt.IsMobileClient(System.Web.HttpRequest, System.Web.HttpRequest)' is a 'method', which is not valid in the given context
Do i have to somehow cast this to the current instance? Should i just be doing this differently all together?
You're trying to call a method like a property. Methods need parentheses:
HttpRequestExt.IsMobileClient().ToString()
^^
You'll also have to call it on the current request, not just statically, as it is an extension method (which takes a parameter). E.g.:
sectionTitle.InnerHtml = Page.Request.IsMobileClient(Page.Response).ToString();
This question already has answers here:
How can I get the assembly last modified date?
(6 answers)
Closed 9 years ago.
How can I retrieve the Created date from the current .NET assembly?
I'd like to add some realy simple functionality where my app stops working one week after the build date of the main assembly. I already wrote the code that kills my app after a given date. I just need to programmatically retrieve the creation date from the assembly.
The following is based on: https://blog.codinghorror.com/determining-build-date-the-hard-way/
public static class ApplicationInformation
{
/// <summary>
/// Gets the executing assembly.
/// </summary>
/// <value>The executing assembly.</value>
public static System.Reflection.Assembly ExecutingAssembly
{
get { return executingAssembly ?? (executingAssembly = System.Reflection.Assembly.GetExecutingAssembly()); }
}
private static System.Reflection.Assembly executingAssembly;
/// <summary>
/// Gets the executing assembly version.
/// </summary>
/// <value>The executing assembly version.</value>
public static System.Version ExecutingAssemblyVersion
{
get { return executingAssemblyVersion ?? (executingAssemblyVersion = ExecutingAssembly.GetName().Version); }
}
private static System.Version executingAssemblyVersion;
/// <summary>
/// Gets the compile date of the currently executing assembly.
/// </summary>
/// <value>The compile date.</value>
public static System.DateTime CompileDate
{
get
{
if (!compileDate.HasValue)
compileDate = RetrieveLinkerTimestamp(ExecutingAssembly.Location);
return compileDate ?? new System.DateTime();
}
}
private static System.DateTime? compileDate;
/// <summary>
/// Retrieves the linker timestamp.
/// </summary>
/// <param name="filePath">The file path.</param>
/// <returns></returns>
/// <remarks>http://www.codinghorror.com/blog/2005/04/determining-build-date-the-hard-way.html</remarks>
private static System.DateTime RetrieveLinkerTimestamp(string filePath)
{
const int peHeaderOffset = 60;
const int linkerTimestampOffset = 8;
var b = new byte[2048];
System.IO.FileStream s = null;
try
{
s = new System.IO.FileStream(filePath, System.IO.FileMode.Open, System.IO.FileAccess.Read);
s.Read(b, 0, 2048);
}
finally
{
if(s != null)
s.Close();
}
var dt = new System.DateTime(1970, 1, 1, 0, 0, 0).AddSeconds(System.BitConverter.ToInt32(b, System.BitConverter.ToInt32(b, peHeaderOffset) + linkerTimestampOffset));
return dt.AddHours(System.TimeZone.CurrentTimeZone.GetUtcOffset(dt).Hours);
}
}
I don't think the assembly itself contains it's creation date. I suspect the closest you can get is the creation date of the assembly file itself:
File.GetCreationTime(Assembly.GetExecutingAssembly().Location)
should do the trick.
EDIT:
I think Jeff Atwood's solution, written up by "grenade" in this thread, is probably the better way to go now.
What's wrong with:
System.IO.File.GetLastWriteTime(Assembly.GetExecutingAssembly().Location);
Maybe this post on coding horror may help
This should work:
var entryAssembly = Assembly.GetEntryAssembly();
var fileInfo = new FileInfo(entryAssembly.Location);
var buildDate = fileInfo.LastWriteTime;
The best way to do this would be with a custom attribute that you set on the PreBuild of your assembly.
And then use the standard reflection to get the attribute you created.
But out of curiosity, why kill the app after the BUILD date?
If you're writing an application for a mobile device using the compact framwork, Assembly.Location is not available.
Here, I found an alternative:
System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase)
I am about to begin reading tons of binary files, each with 1000 or more records. New files are added constantly so I'm writing a Windows service to monitor the directories and process new files as they are received. The files were created with a c++ program. I've recreated the struct definitions in c# and can read the data fine, but I'm concerned that the way I'm doing it will eventually kill my application.
using (BinaryReader br = new BinaryReader(File.Open("myfile.bin", FileMode.Open)))
{
long pos = 0L;
long length = br.BaseStream.Length;
CPP_STRUCT_DEF record;
byte[] buffer = new byte[Marshal.SizeOf(typeof(CPP_STRUCT_DEF))];
GCHandle pin;
while (pos < length)
{
buffer = br.ReadBytes(buffer.Length);
pin = GCHandle.Alloc(buffer, GCHandleType.Pinned);
record = (CPP_STRUCT_DEF)Marshal.PtrToStructure(pin.AddrOfPinnedObject(), typeof(CPP_STRUCT_DEF));
pin.Free();
pos += buffer.Length;
/* Do stuff with my record */
}
}
I don't think I need to use GCHandle because I'm not actually communicating with the C++ app, everything is being done from managed code, but I don't know of an alternative method.
Using Marshal.PtrToStructure is rather slow. I found the following article on CodeProject which is comparing (and benchmarking) different ways of reading binary data very helpful:
Fast Binary File Reading with C#
For your particular application, only one thing will give you the definitive answer: Profile it.
That being said here are the lessons I've learned while working with large PInvoke solutions. The most effective way to marshal data is to marshal fields which are blittable. Meaning the CLR can simple do what amounts to a memcpy to move data between native and managed code. In simple terms, get all of the non-inline arrays and strings out of your structures. If they are present in the native structure, represent them with an IntPtr and marshal the values on demand into managed code.
I haven't ever profiled the difference between using Marshal.PtrToStructure vs. having a native API dereference the value. This is probably something you should invest in should PtrToStructure be revealed as a bottleneck via profiling.
For large hierarchies marshal on demand vs. pulling an entire structure into managed code at a single time. I've run into this issue the most when dealing with large tree structures. Marshalling an individual node is very fast if it's blittable and performance wise it works out to only marshal what you need at that moment.
In addition to JaredPar's comprehensive answer, you don't need to use GCHandle, you can use unsafe code instead.
fixed(byte *pBuffer = buffer) {
record = *((CPP_STRUCT_DEF *)pBuffer);
}
The whole purpose of the GCHandle/fixed statement is to pin/fix the particular memory segment, making the memory immovable from GC's point of view. If the memory was movable, any relocation would render your pointers invalid.
Not sure which way is faster though.
This may be outside the bounds of your question, but I would be inclined to write a little assembly in Managed C++ that did an fread() or something similarly fast to read in the structs. Once you've got them read in, you can use C# to do everything else you need with them.
here's a small class i made a while back while playing with structured files. it was the fastest method i could figure out at the time shy of going unsafe (which was what i was trying to replace and maintain comparable performance.)
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices;
namespace PersonalUse.IO {
public sealed class RecordReader<T> : IDisposable, IEnumerable<T> where T : new() {
const int DEFAULT_STREAM_BUFFER_SIZE = 2 << 16; // default stream buffer (64k)
const int DEFAULT_RECORD_BUFFER_SIZE = 100; // default record buffer (100 records)
readonly long _fileSize; // size of the underlying file
readonly int _recordSize; // size of the record structure
byte[] _buffer; // the buffer itself, [record buffer size] * _recordSize
FileStream _fs;
T[] _structBuffer;
GCHandle _h; // handle/pinned pointer to _structBuffer
int _recordsInBuffer; // how many records are in the buffer
int _bufferIndex; // the index of the current record in the buffer
long _recordPosition; // position of the record in the file
/// <overloads>Initializes a new instance of the <see cref="RecordReader{T}"/> class.</overloads>
/// <summary>
/// Initializes a new instance of the <see cref="RecordReader{T}"/> class.
/// </summary>
/// <param name="filename">filename to be read</param>
public RecordReader(string filename) : this(filename, DEFAULT_STREAM_BUFFER_SIZE, DEFAULT_RECORD_BUFFER_SIZE) { }
/// <summary>
/// Initializes a new instance of the <see cref="RecordReader{T}"/> class.
/// </summary>
/// <param name="filename">filename to be read</param>
/// <param name="streamBufferSize">buffer size for the underlying <see cref="FileStream"/>, in bytes.</param>
public RecordReader(string filename, int streamBufferSize) : this(filename, streamBufferSize, DEFAULT_RECORD_BUFFER_SIZE) { }
/// <summary>
/// Initializes a new instance of the <see cref="RecordReader{T}"/> class.
/// </summary>
/// <param name="filename">filename to be read</param>
/// <param name="streamBufferSize">buffer size for the underlying <see cref="FileStream"/>, in bytes.</param>
/// <param name="recordBufferSize">size of record buffer, in records.</param>
public RecordReader(string filename, int streamBufferSize, int recordBufferSize) {
_fileSize = new FileInfo(filename).Length;
_recordSize = Marshal.SizeOf(typeof(T));
_buffer = new byte[recordBufferSize * _recordSize];
_fs = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.None, streamBufferSize, FileOptions.SequentialScan);
_structBuffer = new T[recordBufferSize];
_h = GCHandle.Alloc(_structBuffer, GCHandleType.Pinned);
FillBuffer();
}
// fill the buffer, reset position
void FillBuffer() {
int bytes = _fs.Read(_buffer, 0, _buffer.Length);
Marshal.Copy(_buffer, 0, _h.AddrOfPinnedObject(), _buffer.Length);
_recordsInBuffer = bytes / _recordSize;
_bufferIndex = 0;
}
/// <summary>
/// Read a record
/// </summary>
/// <returns>a record of type T</returns>
public T Read() {
if(_recordsInBuffer == 0)
return new T(); //EOF
if(_bufferIndex < _recordsInBuffer) {
// update positional info
_recordPosition++;
return _structBuffer[_bufferIndex++];
} else {
// refill the buffer
FillBuffer();
return Read();
}
}
/// <summary>
/// Advances the record position without reading.
/// </summary>
public void Next() {
if(_recordsInBuffer == 0)
return; // EOF
else if(_bufferIndex < _recordsInBuffer) {
_bufferIndex++;
_recordPosition++;
} else {
FillBuffer();
Next();
}
}
public long FileSize {
get { return _fileSize; }
}
public long FilePosition {
get { return _recordSize * _recordPosition; }
}
public long RecordSize {
get { return _recordSize; }
}
public long RecordPosition {
get { return _recordPosition; }
}
public bool EOF {
get { return _recordsInBuffer == 0; }
}
public void Close() {
Dispose(true);
}
void Dispose(bool disposing) {
try {
if(disposing && _fs != null) {
_fs.Close();
}
} finally {
if(_fs != null) {
_fs = null;
_buffer = null;
_recordPosition = 0;
_bufferIndex = 0;
_recordsInBuffer = 0;
}
if(_h.IsAllocated) {
_h.Free();
_structBuffer = null;
}
}
}
#region IDisposable Members
public void Dispose() {
Dispose(true);
}
#endregion
#region IEnumerable<T> Members
public IEnumerator<T> GetEnumerator() {
while(_recordsInBuffer != 0) {
yield return Read();
}
}
#endregion
#region IEnumerable Members
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() {
return GetEnumerator();
}
#endregion
} // end class
} // end namespace
to use:
using(RecordReader<CPP_STRUCT_DEF> reader = new RecordReader<CPP_STRUCT_DEF>(path)) {
foreach(CPP_STRUCT_DEF record in reader) {
// do stuff
}
}
(pretty new here, hope that wasn't too much to post... just pasted in the class, didn't chop out the comments or anything to shorten it.)
It seems this has nothing to do with neither C++ nor marshalling. You know the structure what else do you need.
Obviously you need a simple code which will read group of bytes representing one struct and then using BitConverter to place bytes into corresponding C# fields..