Click here to Skip to main content
15,893,814 members
Please Sign up or sign in to vote.
3.67/5 (3 votes)
See more:
Actually i m looking for source code to how can i make a function to generate guid.
But i know this community is not for our homework,so as much possible help me,in your way.Like logic,some flow,algo and some source code if u like!

Attention##-> I am not looking for guid.newguid().
I want to know how this works,and want to write it's functionality or source code in c# windows.

In Short i just want to know the source code to write a method,which will work like guid.newguid()
Posted
Comments
Thomas Daniels 29-Dec-12 4:47am    
Interesting question.
+5!
StackQ 29-Dec-12 4:49am    
k,thnx,ya i m searching about it since 5 days,but i have not perfect solution,so finally i posted it here.

Omg =)

So you want internal of Guid.NewGuid()?
Here you are:

C#
[SecuritySafeCritical, __DynamicallyInvokable]
public static Guid NewGuid()
{
    Guid guid;
    Marshal.ThrowExceptionForHR(Win32Native.CoCreateGuid(out guid), new IntPtr(-1));
    return guid;
}


where Win32Native is a wrapper for Win32 API
C#
[DllImport("ole32.dll")]
internal static extern int CoCreateGuid(out Guid guid);


SO what prevents you to develop your own solution?
Lest look at this guid: EBD0A0A2-B9E5-4433-87C0-68B6B72699C7
From which parts it was collated ? onle ypercase letters and digit, so:

C#
string def = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
Random rnd = new Random();
Dictionary<int,int> guidGroups=new Dictionary<int,int>();
guidGroups.Add(0,8);
guidGroups.Add(1,4);
guidGroups.Add(2,4);
guidGroups.Add(3,4);
guidGroups.Add(4,12);
StringBuilder ret = new StringBuilder();
for(g=0;g<5;g++)
{
ret.Append("-");
for (int i = 0; i <guidgroups[g]>    ret.Append(def.Substring(rnd.Next(def.Length), 1));
}
return ret.ToString();


This code might not work because i don't verify it, but i have wrote this code with one intent is to help you to develop some basic solution....
But on my opinion i do not prefer to develop vehicle if they are already exists
 
Share this answer
 
v2
Comments
StackQ 17-Jan-13 5:17am    
thnx for helping me!!!!I m trying ur code..
Ofcourse i will also not prefer to develop vehicle,but prefer to learn how we assemble vehicle...
That is a very good question, but in all seriousness, I wouldn't do it myself. It isn't particularly complicated (the V4 spec is basically a 128bit random number) and there is C code which should translate to c# code pretty well here: http://www.gotdotnet.ru/blogs/denish/1965/[^] - the original is in Russian, but Google translate makes a pretty good job of translating it.

The problem is that you then need to examine the results and work out how "unique" your GUID is (the link goes into that as well) - if you don't have to do this for homework, then stick to the library versions - they are tested and a lot easier!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900