unit1 summary

Download Unit1 summary

Post on 13-Apr-2017




0 download

Embed Size (px)


Unit 1 Summary


Unit 1 Summary


P/Invoke, Marshalling

C++ .h fileextern "C"{struct EndpointData{char* userName1;char* userName2;};

struct ResultData{char* userName;int userId;};

__declspec(dllexport) int __stdcall findPath(EndpointData* pStructs, int nItems, const char* userName1, const char* userName2);__declspec(dllexport) int __stdcall returnNumber();__declspec(dllexport) ResultData* __stdcall findPathWithResultData(EndpointData* pStructs, int nItems, const char* userName1, const char* userName2, int& resultArraySize);}

C# methods definition[DllImport("ExternalNativeLibrary.dll")]public static extern int returnNumber();

[DllImport("ExternalNativeLibrary.dll")]public static extern int findPath(MyStruct[] pStructs, int nItems, string userNamr1, string userName2);

[DllImport("ExternalNativeLibrary.dll", EntryPoint = "findPathWithResultData")]public static extern IntPtr findPathWithResultData(MyStruct[] pStructs, int nItems, string userNamr1, string userName2, out int resultArrSize);

C# structures definition

[StructLayout(LayoutKind.Sequential)] public struct MyStruct { public string userName1; public string userName2; }

[StructLayout(LayoutKind.Sequential)] public struct Results { public string userName; public int userId; }

C# Call methods

Console.WriteLine("Return number:");Console.WriteLine(returnNumber());int count;var pointer = findPathWithResultData(structArray, structArray.Length, "user1", "user3", out count);

var firstResult = Marshal.PtrToStructure(pointer);

var pointer2 = pointer + Marshal.SizeOf();var secondResult = Marshal.PtrToStructure(pointer2);

SerializationSerialization is the process of converting an object into a stream of bytes in order to storeortransmitit.Deserialization is the opposite process to serialization converting data to object.

The most used serialization typesin .NET Framework:

Binary serialization (is the serialization process with binary data as a result)Xml serialization (is the serialization process with XML data as a result)

Serialization example public static class XmlSerializationService where T : class{ private static XmlSerializer _serializer = new XmlSerializer(typeof(T));

public static T Deserialize(string serializedObject) { using (var sr = new StringReader(serializedObject)) { return (T)_serializer.Deserialize(sr); } }

public static string Serialize(T objectToSerialize) { using (var sw = new StringWriter()) { _serializer.Serialize(sw, objectToSerialize); return sw.ToString(); } } }


Project architecture

Project requirements: , = .

C++ . . .C# . ., . . ++ . .\ . csv : exception handling, logs, application menu.


Inspired by Technology.Driven by Value.


What to readP/Invoke:https://msdn.microsoft.com/en-us/library/aa288468(v=vs.71).aspxhttps://blogs.msdn.microsoft.com/jonathanswift/2006/10/02/calling-an-unmanaged-dll-from-net-c/https://msdn.microsoft.com/en-us/library/26thfadc(v=vs.110).aspx