|
AseelHadlaq wrote: How can I use it ?
By learning how to use it of course.
You can google for information on it.
|
|
|
|
|
Yea I noticed it was a stupid question, I read about it and I found it a bit confusing and I didnt get the concept of it, can you give me a link where it is explained in a clear way ? and can I send and retrieve variables through interop ?
thank you
|
|
|
|
|
I can't figure out for you if it is clear. You can use the following in google to search for links that would most likely be of help to you.
"C#" interop example -com
This is dependent on you knowing a non-trivial amount of C and having some non-trivial experience in writing code. If you have neither then this is going to be a difficult task. Especially since if you mess it up the most likely outcome will be a system exception and the C# VM will exit will little information.
|
|
|
|
|
All converters will offer a free trial/demo version. Try it out and compare the results with some common sense. Even if the conversion is less than 100%, it may give you something you would have missed.
The main challenge of converting C/C++ code to other languages, especially with pointers, is that a C/C++ declaration alone does not tell you how something is used. This is in contrast to C# where the declaration does tell you all you need to know for a translation to other languages.
David Anton
Convert between VB, C#, C++, & Java
www.tangiblesoftwaresolutions.com
Instant C# - VB to C# Converter
Instant VB - C# to VB Converter
|
|
|
|
|
thnx I found it earlier but it's not free and I dont know about its reliability , I guess I have to try it
|
|
|
|
|
Conversion isn't usually that difficult once you get underway.
Pointers are fairly straight forward.
In C# all classes are 'reference' types, meaning that a pointer (actually a copy of the pointer) is passed, not the value - so these are often directly interchangeable with C code when translating.
structs are 'value' types so the value is copied rather than the reference. This can be changed by using out or ref to retrieve or pass a pointer.
Only if there is pointer arithmetic involved or pointers to pointers do matters get more complicated. Much of the time, by carefull examination of the code and refactoring it into smaller sections, you will find you can get rid of the pointers altogether
Good luck, and if you get stuck with any individual parts feel free to post.
|
|
|
|
|
That is scary a bit but I have to try ,, thank you
|
|
|
|
|
Use DllImport to get your C functions into your C# project.
If you actually wrote C++ objects, you can create a wrapper for them in managed C++, and the managed C++ dll can be called from you C# code.
|
|
|
|
|
My questions will sound stupid I know
But can I send and retrieve variables ? without changing my C code ?
can you explain more about it plzz
|
|
|
|
|
You might want to start here[^].
AseelHadlaq wrote: But can I send and retrieve variables ? without changing my C code ?
That really depends on how your C is written. In 10 years of working with .NET, I've never found a case where I couldn't P/Invoke because of variables, but that's not to say that some weird and wacky pointer manipulation couldn't cause an issue (although I do recall dealing with a particularly tricky void** parameter that caused some real headaches).
|
|
|
|
|
Hello all. I think I'm over using DirectoryEntry but I'm not sure how to do what I want in any other way.
What this tool is supposed to do is connect to the machines listed in AD and retrieve the members of the "Administrators" group on the local machine.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.DirectoryServices;
using System.DirectoryServices.ActiveDirectory;
using System.Data;
using System.IO;
using System.Xml;
namespace FindAdmins
{
class Program
{
static void Main(string[] args)
{
string connectionPrefix = "LDAP://" + FriendlyDomainToLdapDomain(Environment.UserDomainName);
DirectoryEntry entry = new DirectoryEntry(connectionPrefix);
DirectorySearcher mySearcher = new DirectorySearcher(entry);
mySearcher.Filter = "(objectClass=computer)";
mySearcher.PageSize = 50000;
mySearcher.SizeLimit = 100000;
mySearcher.Asynchronous = true;
mySearcher.PropertiesToLoad.Add("cn");
SearchResultCollection results = mySearcher.FindAll();
Console.WriteLine("Number of machines found: " + results.Count);
int count = 0;
DataTable table = new DataTable();
table.TableName = Environment.UserDomainName;
table.Columns.Add("Machine");
table.Columns.Add("Group");
table.Columns.Add("User");
foreach (SearchResult result in results)
{
count++;
Console.Write(count + "\r");
try
{
DirectoryEntry remMachine = new DirectoryEntry("WinNT://" + result.Properties["cn"][0].ToString() + ",Computer");
DirectoryEntry admGroup = remMachine.Children.Find("administrators", "group");
object members = admGroup.Invoke("members", null);
foreach (object groupMember in (IEnumerable)members)
{
DirectoryEntry member = new DirectoryEntry(groupMember);
if (member.SchemaClassName == "Group")
{
object admMembers = member.Invoke("members", null);
foreach (object admMember in (IEnumerable)admMembers)
{
DirectoryEntry membername = new DirectoryEntry(admMember);
DataRow dr = table.NewRow();
dr["Machine"] = remMachine.Name.ToString();
dr["Group"] = member.Name.ToString();
dr["User"] = membername.Name.ToString();
table.Rows.Add(dr);
membername.Dispose();
}
}
else if (member.SchemaClassName == "User")
{
DataRow dr = table.NewRow();
dr["Machine"] = remMachine.Name.ToString();
dr["Group"] = "Administrators";
dr["User"] = member.Name.ToString();
table.Rows.Add(dr);
member.Dispose();
}
}
admGroup.Dispose();
remMachine.Dispose();
GC.Collect();
}
catch (Exception e)
{
}
}
table.WriteXml(@"C:\admins.xml", true);
mySearcher.Dispose();
entry.Dispose();
}
public static string FriendlyDomainToLdapDomain(string friendlyDomainName)
{
string ldapPath = null;
try
{
DirectoryContext objContext = new DirectoryContext(DirectoryContextType.Domain, friendlyDomainName);
Domain objDomain = Domain.GetDomain(objContext);
ldapPath = objDomain.Name;
}
catch (DirectoryServicesCOMException e)
{
ldapPath = e.Message.ToString();
}
return ldapPath;
}
}
}
|
|
|
|
|
And your question is?
One of these days I'm going to think of a really clever signature.
|
|
|
|
|
LOL, forgot the question.
So, I'm thinking my implementation is leaky and slow due to the over usage for DirectoryEntry. IS there a less memory leaky and faster way to do this?
|
|
|
|
|
Erick Kinnee wrote: IS there a less memory leaky and faster way to do this? No idea. That's what development is about, finding the answers.
One of these days I'm going to think of a really clever signature.
|
|
|
|
|
What is "over usage"? My apps are using a lot of ints, do you think it could leak memory due to that? It won't, and speed is not an indication for a memory-leak. The amount of used memory is an indication.
A few remarks on the code;
if (member.SchemaClassName == "Group")
{
object admMembers = member.Invoke("members", null);
foreach (object admMember in (IEnumerable)admMembers)
{
DirectoryEntry membername = new DirectoryEntry(admMember);
DataRow dr = table.NewRow();
dr["Machine"] = remMachine.Name.ToString();
dr["Group"] = member.Name.ToString();
dr["User"] = membername.Name.ToString();
table.Rows.Add(dr);
membername.Dispose();
}
}
else if (member.SchemaClassName == "User")
{
DataRow dr = table.NewRow();
dr["Machine"] = remMachine.Name.ToString();
dr["Group"] = "Administrators";
dr["User"] = member.Name.ToString();
table.Rows.Add(dr);
member.Dispose();
}
Reads to me as;
if (something)
membername.Dispose();
else
member.Dispose();
I'd expect something along these lines;
foreach (object groupMember in (IEnumerable)members)
{
using (var member = new DirectoryEntry(groupMember))
{
if (member.SchemaClassName == "Group")
..
You could gain some speed perhaps, if you had a thread for each result in there. Depends on how many results and how many cores your CPU got. A progressbar is often appreciated for longer-running tasks.
Bastard Programmer from Hell
if you can't read my code, try converting it here[^]
|
|
|
|
|
Thank you for your answer.
I disable AppDomain.GetCurrentThreadId(), but I continue to get error.
Regards
|
|
|
|
|
So what you want? Be more specific..
|
|
|
|
|
Saridakis Manolis wrote: Thank you for your answer. I disable
AppDomain.GetCurrentThreadId(), but I continue to get error.
What answer? Is this from some other thread? If so, you should continue on that thread.
|
|
|
|
|
The AppDomain will not "hide" the exception as "Application.OnException" does. If you throw an exception, the thing will die.
You could wrap the dangerous part in a try..catch block, catching the exceptions that you expect.
Bastard Programmer from Hell
if you can't read my code, try converting it here[^]
|
|
|
|
|
Does anyone know why this code doenst work?(In a consonle Application)
it cant catch exceptions
static void Main(string[] args)
{
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(MyException);
throw new ArgumentException("for test");
}
static void MyException(object sender, UnhandledExceptionEventArgs e)
{
Console.WriteLine(e.ExceptionObject);
}
}
Thanks
Of one Essence is the human race
thus has Creation put the base
One Limb impacted is sufficient
For all Others to feel the Mace
(Saadi )
|
|
|
|
|
What do you mean "doesn't work"?
I just tried it and it does what I would expect.
Ideological Purity is no substitute for being able to stick your thumb down a pipe to stop the water
|
|
|
|
|
Im wonderful it cant catgh any unhandled exceptions and clr shows unhandled exception windows when I see event viewer it shows these info about my program
Faulting application name: Mydynamic.exe, version: 1.0.0.0, time stamp: 0x507ac6d7
Faulting module name: KERNELBASE.dll, version: 6.1.7601.17514, time stamp: 0x4ce7bafa
Exception code: 0xe0434352
Fault offset: 0x0000b727
Faulting process id: 0x4b4
Faulting module path: F:\Windows\syswow64\KERNELBASE.dll
Report Id: 81b9c3ff-1608-11e2-bc0d-005056c00008
I dont have this problem in other programs(like WPF,Windows Forms or ASP.Net) it works for them and donest work for console applications
Of one Essence is the human race
thus has Creation put the base
One Limb impacted is sufficient
For all Others to feel the Mace
(Saadi )
|
|
|
|
|
My complete test code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(MyException);
throw new ArgumentException("for test");
}
static void MyException(object sender, UnhandledExceptionEventArgs e)
{
Console.WriteLine("Exception Caught!");
Environment.Exit(0);
}
}
} My complete CMD output:
Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation. All rights reserved.
C:\Users\griff>d:
D:\>cd \temp
D:\Temp>ca
Exception Caught!
D:\Temp>
What am I doing that differs from you?
Ideological Purity is no substitute for being able to stick your thumb down a pipe to stop the water
|
|
|
|
|
My code is the same with your code except (Environment.Exit(0); (I dont have this code) it works with this code but without it I got this message of e.ExceptionObject
System.ArgumentException: for test
at ConsoleApplication1.Program.Main(String[] args) in c:\users\apple\documents\visual studio 2010\Projects\Mydynamic\Mydynamic\Program.cs:line 17
and it shows this window
Windows can check online for a solution to the problems.
Check online for a solution and close the program
Close the program
Debug the problem
when I click on the "Debug the problem" item, visual sutdio Just In-Time debugger asks "Do you want to debug using the selected debugger?" after yes it goes to my code exception details are
System.ArgumentException was unhandled
Message=for test
Source=Mydynamic
StackTrace:
at ConsoleApplication1.Program.Main(String[] args) in c:\users\apple\documents\visual studio 2010\Projects\Mydynamic\Mydynamic\Program.cs:line 17
InnerException:
Of one Essence is the human race
thus has Creation put the base
One Limb impacted is sufficient
For all Others to feel the Mace
(Saadi )
|
|
|
|
|
That is pretty much what I would expect. If you don't have the exception handler cause an program exit, then the system has no choice except to cause an abnormal termination - which is what your message shows.
Think about it: the exception is thrown from the Main method. The application cannot continue under any circumstances without restarting the whole program.
In Winforms et al., it is multithreaded, so a single thread can be terminated without necessarily killing the whole app - but your console app is a single thread. If it can't continue the only running thread, it can't continue the application.
In GUI apps, the Main function is executed in a thread, which fires off other threads to handle the UI for each form. They can be terminated without affecting the Main thread, since all it is doing is waiting for the others to die anyway!
Ideological Purity is no substitute for being able to stick your thumb down a pipe to stop the water
|
|
|
|