How to programmatically clear the Kerberos ticket cache - c#

Does anyone know how to clear out the Kerberos ticket cache on the local computer - using managed \ unmanaegd code?
Thanks in advance!

I believe you need to do a call to LsaCallAuthenticationPackage using KERB_PURGE_TKT_CACHE_REQUEST after using either LsaConnectUntrusted or LsaRegisterLogonProcess. Sorry no specifics, but I don't have my code for this around...

The most simple way is to take the source code of Microsoft's KList (Included in the platform SDK\ Samples), and to do like them...

There is a "managed C#" example of purging kerberos tickets using pInvoke at https://github.com/ErtiEelmaa/StackOverflow/blob/master/GroupPolicyUtilities.cs
Way too long to copy/paste here. It took some time since structures in pInvoke were invalid(eg someone though C++ LONG is equal to C# LONG and yada-yada), however, I've tested this one, and it worked for me.
Also, I noticed that the Windows server 2003 klist.c has few bugs in PurgeTicket:
it does not clean up the response
it double-checks the same "RESULT", instead of checking "RESULT" and then "SUB-RESULT"

The simple, stupid way:
system("kdestroy");
Or if you want to be more legit, just check out the source of a kdestroy implementation. krb5_cc_destroy() seems to be the relevant function call.

Related

Any way to get the DNS cache entries in C#?

I've been looking for answers and couldn't quite find anything that worked for me, hopefully there's someone who can help me out with this.
I need to get the number of entries in the DNS cache of the computer. Not really even interested in the particular entries, just the total count.
This answer (Is there a way to monitor the system's DNS cache on c#?) comes close but I can't manage to download the powershell module to try to get it to work, plus it would be best if I could get a solution that worked without requiring any extra tools to be installed.
Thanks in advance.
I have managed to do it by using the PowerShell Get-DnsClientCache function.
DnsClient functions are apparently loaded by default in the non-server Windows OSs as well, unlike the dnsserver module.
Anyone interested can see an example on how to execute PowerShell scripts in the link posted in the original question.

Using Ymodem file transfer in c#

I am currently searching for an idea to use a ymodem communication on one of my software using c# Framework .NET 2.0.
Does somebody have a solution to do ymodem file transfer in C# ? Because I haven't found anything for now...
Thank you by advance.
Joe
There is a project on google code (http://code.google.com/p/ymodemdotnet/). I can not speak to it's quality. It would be a start at least.
Here is an Ymodem implementation I wrote.
It's still far from perfect, but it works, commented and simple.
https://sites.google.com/site/adamficsor1024/home/programming/xymodem
I don't know about YModem, but original XModem (not the non-standard 1k version) is pretty easy to code up on your own with a SOH byte, block#, block, and small checksum.

Hook into the Windows File Copy API from C#

Is there a way to hook into the Windows File Copy API from C#? I'm aware this would require unmanaged code, but a code sample or starter would be helpful. I've already seen the C++ code, but it's all greek.
UPDATE: I apologize, I should have been more clear about my intentions. I wish to actually change the copy feature of Windows to be more rigid (e.g. allow queing, scheduling, handle restarts, pauses, etc.). When I said hook, I meant API hook so that when someone starts a copy I get the sources and destinations and can handle it to my heart's desire. I'm old school and used to hook the Mac OS API a lot to do these things so I assumed that in the C++ WINAPI world there was some type of equiv.
Update:
As others have stated, why not just use System.IO.File.Copy(...)? It calls this same underlying API. As Michael G points out, perhaps you intend to call the the FileCopyEx API that allows you to hook progress-indication callbacks(???) That's really the only reason to P/Invoke file-copy stuff in .NET. Details on how to implement FileCopyEx that can be found here: http://pinvoke.net/default.aspx/kernel32/CopyFileEx.html
Original answer: (which you really shouldn't use...)
Code snippet removed because you really shouldn't use it...
If you're hell-bent on making busted-code, you can find out how to use it at: Found at http://pinvoke.net/default.aspx/kernel32/CopyFile.html
I wish to actually change the copy feature of Windows to be more rigid
You shouldn't do that in managed code, because of the same reasons you should not write managed shell extensions.
You can do so by calling System.IO.File.Copy. Its internal implementation already uses the Windows API.
Edit: File.Copy also handles permissions correctly and has the benefit of throwing an exception with meaningful data if something fails, so you don't have to manually check and analyze the return status.
You can use Deviare API Hook that lets you intercept any API from .NET and read parameters using VARIANT types. There is a full example very easy to follow in C#.
The other benefit of using unmanaged Copy File API is the ability to have a progress callback.
Note: as stated in other answers, I would use the managed version of File.Copy as it's safer, and can usually do everything you require.

How to find if app has been installed before?

Is it possible for a .NET application to leave a trace so that it can be found if the application is re-installed?
Of course, a trace that is difficult to be removed.
Create a file
Create a registry key
Create a global variable
A combination of all above
...and then check for existence the next time install is attempted.
While it is better practice to remove applications in their entirety, I assume this is for 'trial' software (one time install) purposes or a similar reason.
Well, generally, if you un-install something, you'd hope it completely removes itself, and doesn't leave a trace of the fact that it was there. Otherwise, if it doesn't, it hasn't really un-installed.
This is language-agnostic anyway.
So the answer is: Yes, but don't do that.
What problem are you really trying to solve?
Many application leave behind traces (eg in registry) to detect previous installation. However tools like RevoUninstaller can be used to completely remove those traces. One easy way of doing so is to send the machine id to your server.
I've done some Googling to find better information (with no success) but I can remember mention of something referred to as the "Manufacturers Section" of a hard drive, an area which is outside that of normal storage to which it's possible to write information that won't be lost if the drive is re-formatted. The specific product I'm remembering was AutoCAD.
The fact that you are asking for something that is "difficult to be removed" leads me to believe that you are looking for something like this?
It's unlikely that the use of this sort of technology would make you very popular with your customers, particularly with the bad feeling directed towards DRM, rootkits and the like.

How to turn off *multi display* in c#?

There are many articles about "how to turn off monitors in c#", but they all turn off all the monitors. Is there any possible way to turn off a particular monitor?
A little bit of research lead me here:
http://www.codeguru.com/forum/showthread.php?p=1011720#post1011720
Do you know how to call a function like that in c#?
-- Edit
Looks like this guy has done it all for you:
http://www.codeproject.com/KB/dotnet/Display_Settings.aspx
For calling Win32 Functions not exposed in the .NET Framework
PInvoke.net
is a good ressource.
The function mentioned in the link of silky can be found here.
Also have a look at the used data types.
Just copy the code mentioned in "C# Signature" to your project.
Given that the monitor belongs to the user, not you, why do you want to turn it off.
This is just the sort of think I think a OS should protect me from when a application tries it, so expect problems…

Categories