|
|
actually, all the free web services seems to reject request after it reaches certain daily hits. which in my case is true. if google crawls my site i am using the service to get the country code so i gave up on using free webservice to get a country code based on IP. I used this one http://dotnetguts.blogspot.com/2008/06/finding-country-from-visitors-ip-in.html[^]
but its getting a wrong country codes for example in below. I am using my iphone which is in sydney, australia, its logging country code as FR. could someone please help?
58.104.249.183 FR Mozilla/5.0 (iPhone; U; CPU iPhone OS 3_0 like Mac OS X; en-us) AppleWebKit/528.18 (KHTML, like Gecko) Version/4.0 Mobile/7A341 Safari/528.16
|
|
|
|
|
good question. 5!
some one has a solution here.
Another solution here.
♫ 99 little bugs in the code,
99 bugs in the code
We fix a bug, compile it again
101 little bugs in the code ♫
|
|
|
|
|
Hi everyone,
My name is Susan and I work for an outside vendor who was hired by SAP BusinessObjects to learn more about what people are saying about CrystalReports in the social media space. I was hoping you could give me some feedback on what your initial thoughts are about CrystalReports. Do you like the product? Do you have any issues with the product? What are your reasons for coming to this message board?
Any feedback would be much appreciated. Thank you!
- Susan
|
|
|
|
|
This would be better asked in The Lounge[^] IMO as it's not really a programming question directly related to C#
Dave
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn) Why are you using VB6? Do you hate yourself? (Christian Graus)
|
|
|
|
|
Thanks Dave, I'll post there!
- Susan
|
|
|
|
|
I am wondering how to create a key container on a machine that only my application can access. I have successfully created and used the container, but I think that other users and applications on the machine have access to the private key. Is there any way to protect this information? The code used to generate and utilize the CspParameters object is below:
CspParameters csp = new CspParameters();
csp.Flags = CspProviderFlags.UseMachineKeyStore |
CspProviderFlags.UseNonExportableKey;
csp.KeyContainerName = "Example Container Name";
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(1024, csp); Thanks,
Sounds like somebody's got a case of the Mondays
-Jeff
|
|
|
|
|
Guys,
I`m working on a Windows application and running into a problem, can`t seem to figure it out. Let me first explain the app. Its a windows forms application with a Tab Controls. Each Tab Control is a single file and has other controls within it [tabcontrols, buttons, etc etc]. Based on the file extension only the First tab within the Tab Control changes everything else remains the same. Lets say like a Web browser with Tabs.
The problem I`m running into is - How do I stop duplicate tabcontrols [each tab control is a file] from opening. Lets say I load a file ABC.opt how do make sure ABC.opt is not loaded again..i.e, open by the software. I can load files now, but can`t seem to control the duplicates.
Also, when i Save a file..I want only the contents of the Tab Control open to be saved. Is there a Tab Control active property? What is the best way to do it. Something like a Web Browser..Lets say you open a website in a tab and click on Save on the Menu Strip, only the active Tab is saved.
Here is the code I`m working on.
//File Item Clicked from the MainMenuItem - This has File New, File Open, Save, Save As, etc
private void FileToolStripMenuItem_DropDownItemClicked(object sender, ToolStripItemClickedEventArgs e)
{
FileToolStripMenuClicked(e.ClickedItem);
}
//FTSI - File Tool Strip Item Clicked
private void FileToolStripMenuClicked(ToolStripItem FTSI)
{
switch (FTSI.Name)
{
case "FileOpenTSMI":
// FileOpen.cs
FileOpen FO = new FileOpen();
FO.OpenFile();
break;
case "FileSave"
//See comments in ABCOpen
//Code here
break;
default:
break;
}
}
//Based on the Extention of the file the ABCOpen() or XYZOpen() functions are called
internal void OpenFile()
{
OpenFileDialog OFD = new OpenFileDialog();
OFD.InitialDirectory = @"C:\Cust";
OFD.Filter = "ABC Files|*.abc|XYZ Files|*.xyz";
OFD.CheckFileExists = true;
OFD.CheckPathExists = true;
OFD.RestoreDirectory = true;
if (OFD.ShowDialog() == DialogResult.OK)
{
switch (Path.GetExtension(OFD.FileName).Substring(1))
{
case "abc":
ABCOpen(OFD.FileName);
break;
case "xyz":
XYZOpen("OFD.FileName");
break;
default:
break;
}
}
}
private void ABCOpen(string FileName)
{
// Code here loads some User Controls
// Code here reads a File and then loads the data into Windows forms
// How do I save it in such a way that only the active tab control data is saved
// Lets say I loaded Sample.abc and Test.xyz. I edit Sample.abc and it is the active Tab, and when I hit save how can I make sure only Sample.abc data is written into the file?
}
Any ideas, help are greatly appreciated.
Thanks!
|
|
|
|
|
sharat567 wrote: how do make sure ABC.opt is not loaded again.
Maintain a collection that contains the opened files and check it before loading a file. It's not that difficult.
I know the language. I've read a book. - _Madmatt
|
|
|
|
|
Determine which tab is active:
See TabControl.SelectedIndex[^] and TabControl.SelectedTab[^]
Determine if a file is already open:
Set the TabPage.Tag property on each TabPage that you open to the full path of the file, then iterate through each tab and compare the full path of the file attempting to be opened. This also gives an easy way to remember the path when you go to save the active tab.
Sounds like somebody's got a case of the Mondays
-Jeff
|
|
|
|
|
I have a cryptography question that is puzzling me. Basically, I am attempting to create an encrypted file that includes a signed hashcode to ensure that it is not tampered with. The procedure to perform this is as follows:
1) Load an asymmetric algorithm (RSACryptoServiceProvider) stored using a CspParameters object
2) Create a symmetric algorithm object (AesCryptoServiceProvider)
3) Encrypt the concatenated key and initialization vector using the RSA encryptor
4) Create a new MemoryStream object (with associated BinaryWriter)
5) Write the following data to the output stream:
*The size of the encrypted key/IV vector
*The encrypted key/IV vector
6) Create new CryptoStream and BinaryWriter objects and write my data to the MemoryStream
7) Hash the MemoryStream, and sign the hash
8) Open a file for writing (FileStream)
9) Write the following data
*Length of the signed hash
*The signed hash
*COPY THE CONTENTS OF THE MEMORY STREAM TO THE OUTPUT
I put the last portion of step 9 in bold because this is where something is going terribly wrong. My entire encryption/decryption process works when I copy/paste the code that writes to the MemoryStream object. However, if I simply attempt to copy the contents of the MemoryStream to the FileStream, the decryption process begins, but I get to a point reading the data where I get the error, "Padding is invalid and cannot be removed". Everything up to this point is decrypted correctly, and my file is validated against the signed hash (in both cases... ie, when I rewrite the data to the FileStream, my file has the same signed hash as when I copy the contents of the MemoryStream to the FileStream). What am I missing here? Why is the file validated against the hash in both cases, but is only able to be decrypted when I rewrite the data? I have copied what I think is relevant below:
class ExampleForm : Form {
private readonly int m_SymmetricKeySize = s_KeySizeSym >> 3;
private readonly int m_SymmetricBlockSize;
private readonly RSACryptoServiceProvider m_AsymmetricAlg;
private readonly SymmetricAlgorithm m_SymmetricAlg = new AesCryptoServiceProvider();
private readonly HashAlgorithm m_HashAlg = new MD5CryptoServiceProvider();
private readonly string m_OutFile =
Path.Combine(Application.CommonAppDataPath, "Example.dat");
private static void CopyStream(Stream src, Stream dst) {
const int bufferSize = 256;
byte[] buffer = new byte[bufferSize];
int count;
do {
count = src.Read(buffer, 0, bufferSize);
dst.Write(buffer, 0, count);
} while (count == bufferSize);
}
public ExampleForm() {
m_SymmetricBlockSize = m_SymmetricAlg.BlockSize >> 3;
CspParameters csp = new CspParameters();
csp.KeyContainerName = "Example Container";
m_AsymmetricAlg = new RSACryptoServiceProvider(2048, csp);
bool isFileValid = File.Exists(m_OutFile);
if (isFileValid) {
Stream dataStream = new MemoryStream();
Stream fileStream = File.OpenRead(m_OutFile);
BinaryReader binaryReader = new BinaryReader(fileStream);
byte[] sign = new byte[binaryReader.ReadInt32()];
fileStream.Read(sign, 0, sign.Length);
ExampleForm.CopyStream(fileStream, dataStream);
binaryReader.Close();
fileStream .Close();
byte[] hash = m_HashAlg.ComputeHash(dataStream);
isFileValid = m_AsymmetricAlg.VerifyHash(hash,
CryptoConfig.MapNameToOID("MD5"), sign);
if (isFileValid) {
dataStream.Seek(0, SeekOrigin.Begin);
binaryReader = new BinaryReader(dataStream);
byte[] encKeyIV = new byte[binaryReader.ReadInt32()];
dataStream.Read(encKeyIV, 0, encKeyIV.Length);
byte[] keyAndIV = new byte[m_SymmetricBlockSize + m_SymmetricKeySize];
keyAndIV = m_AsymmetricAlg.Decrypt(encKeyIV, true);
byte[] key = new byte[m_SymmetricKeySize ];
byte[] iv = new byte[m_SymmetricBlockSize];
for (int idx = 0; idx < m_SymmetricKeySize; ++idx)
key[idx] = keyAndIV[idx];
for (int idx = 0; idx < m_SymmetricBlockSize; ++idx)
iv [idx] = keyAndIV[idx + m_SymmetricKeySize];
CryptoStream cryptoStream = new CryptoStream(dataStream,
m_SymmetricAlg.CreateDecryptor(key, iv), CryptoStreamMode.Read);
BinaryReader cryptoReader = new BinaryReader(cryptoStream);
binaryReader.Close();
cryptoReader.Close();
cryptoStream.Close();
} else {
}
dataStream.Close();
}
}
private void ExampleForm_FormClosing(object sender, FormClosingEventArgs e) {
byte[] keyAndIV = new byte[m_SymmetricBlockSize + m_SymmetricKeySize];
m_SymmetricAlg.Key.CopyTo(keyAndIV, 0);
m_SymmetricAlg.IV .CopyTo(keyAndIV, m_SymmetricKeySize);
Stream dataStream = new MemoryStream();
BinaryWriter binaryWriter = new BinaryWriter(dataStream);
BinaryWriter cryptoWriter = new BinaryWriter(new CryptoStream(
dataStream, m_SymmetricAlg.CreateEncryptor(), CryptoStreamMode.Write));
byte[] encKeyIV = m_AsymmetricAlg.Encrypt(keyAndIV, true);
binaryWriter.Write(encKeyIV.Length);
binaryWriter.Flush();
dataStream.Write(encKeyIV, 0, encKeyIV.Length);
cryptoWriter.Flush();
byte[] hash = m_HashAlg.ComputeHash(dataStream);
byte[] sign = m_AsymmetricAlg.SignHash(hash, CryptoConfig.MapNameToOID(s_HashName));
#region Option 1 (What I would like to do)
Stream fileStream = File.OpenWrite(m_OutFile);
binaryWriter = new BinaryWriter(fileStream);
binaryWriter.Write(sign.Length);
binaryWriter.Flush();
fileStream.Write(sign, 0, sign.Length);
dataStream.Seek(0, SeekOrigin.Begin);
ExampleForm.CopyStream(dataStream, fileStream);
cryptoWriter.Close();
binaryWriter.Close();
fileStream.Close();
dataStream.Close();
#endregion
#region Option 2 (The option that currently works)
cryptoWriter.Close();
binaryWriter.Close();
dataStream .Close();
Stream fileStream = File.OpenWrite(m_OutFile);
binaryWriter = new BinaryWriter(fileStream);
cryptoWriter = new BinaryWriter(new CryptoStream(
fileStream, m_SymmetricAlg.CreateEncryptor(), CryptoStreamMode.Write));
binaryWriter.Write(sign.Length);
binaryWriter.Flush();
fileStream.Write(sign, 0, sign.Length);
binaryWriter.Write(encKeyIV.Length);
binaryWriter.Flush();
fileStream.Write(encKeyIV, 0, encKeyIV.Length);
cryptoWriter.Close();
binaryWriter.Close();
fileStream .Close();
#endregion
}
} Again, I am wondering why "Option 1" from above doesn't work. I noticed that the last character written when copying the stream was an "11". Any ideas? Thanks,
Sounds like somebody's got a case of the Mondays
-Jeff
|
|
|
|
|
I noticed that the size of the two files is different... The one created using "Option 1" is 1144 bytes and the one created using "Option 2" is 1160 bytes. Perhaps this will help someone explain to me what is happening? Thanks again,
Sounds like somebody's got a case of the Mondays
-Jeff
|
|
|
|
|
I don't use the CryptoStream (I have an article about a SecureStream, but I think it will not work for you) but as I remember there is a FlushFinalBlock in the CryptoStream. I think you should use it because I think the "FinalBlock" will include additional information that is needed during decryption.
|
|
|
|
|
Thanks... the FlushFinalBlock() operation you suggested worked! Thank you very much!
Sounds like somebody's got a case of the Mondays
-Jeff
|
|
|
|
|
I use Microsoft.DirectX.AudioVideoPlayback for video player, and I don't know how to get frequencies and amplitude of the sound (when playing video) to draw a waveform. I can't find any-code about it. Somebode help me please!!
Thank you for your help.
|
|
|
|
|
This article[^] demonstrates how to resolve the frequencies and amplitudes of audio waveforms using a FFT algorithm, and even provides code for displaying it. As to accessing the actual audio sample stream, you'll have to look into the DirectX API yourself; I have no idea how it works. I'm sure it's encoded in some way to include synchronized audio and video packet streams for your multimedia hardware to display it correctly, and that will have to be converted to a stream of instantaneous audio samples at a uniform sample rate to make the FFT algorithm work. If the audio is time-compressed it will have to be expanded, and if amplitude compressed an inverse transform will have to be applied to restore the original dynamic range.
By the way, Google led me straight back here. There is an extensive collection of member articles about audio and video manipulation available at CodeProject, and if you haven't already done so, it would be very helpful to you to spend some time reading them. Some contain far more information than their titles would imply.
Good luck, and I look forward to your article appearing once you solve this one.
"A Journey of a Thousand Rest Stops Begins with a Single Movement"
|
|
|
|
|
Hi All,
how find all used / connected COM PORT on the system??
Thanks.
|
|
|
|
|
First, include System.IO.Ports this snippet shout help you:
foreach (string portName in SerialPort.GetPortNames())
{
SerialPort port = new SerialPort(portName);
Console.WriteLine("{0} is open = {1}", port.PortName, port.IsOpen);
}
The important thing is the SerialPort object, it allows communication with the port and has plenty of useful properties and methods see http://msdn.microsoft.com/en-us/library/system.io.ports.serialport(VS.80).aspx[^] for more details.
CCC solved so far: 2 (including a Hard One!)
37!?!! - Randall, Clerks
|
|
|
|
|
Thanks!
|
|
|
|
|
Good morning.
I am trying to detect the music button events from the keyboard, but have been unsuccessful so far. I have the following code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.IO;
using System.Collections;
namespace KPlayer
{
public partial class Form1 : Form
{
WMPLib.WindowsMediaPlayer wmp = new WMPLib.WindowsMediaPlayer();
WMPLib.IWMPPlaylist pl;
WMPLib.IWMPMedia media;
ArrayList ar = new ArrayList();
int intCount = 0;
List<int> numbers = new List<int>();
float currentSize = 10;
string currentFont = "DS-Digital";
public Form1()
{
InitializeComponent();
}
[STAThread]
[DllImport("user32.dll")]
public static extern int SetWindowsHookEx(int hookType,
HookProc callback,
int instance,
int threadID);
[DllImport("user32.dll")]
public static extern int CallNextHookEx(int hookHandle, int code,
int wparam, int lparam);
[DllImport("user32.dll")]
public static extern bool UnhookWindowsHookEx(int hookHandle);
[DllImport("user32.dll")]
public static extern int GetKeyState(int vKey);
int hookHandle = 0;
HookProc cb = null;
public const int WH_KEYBOARD = 2;
public const int HC_ACTION = 0;
public const int HC_NOREMOVE = 3;
public const int VK_CONTROL = 0x11;
public const int VK_LWIN = 0x5B;
public const int VK_RWIN = 0x5C;
public const int VK_APPS = 0x5D;
public const int VK_LSHIFT = 0xA0;
public const int VK_RSHIFT = 0xA1;
public const int VK_LCONTROL = 0xA2;
public const int VK_RCONTROL = 0xA3;
public const int VK_LMENU = 0xA4;
public const int VK_RMENU = 0xA5;
public const int VK_BROWSER_BACK = 0xA6;
public const int VK_BROWSER_FORWARD = 0xA7;
public const int VK_BROWSER_REFRESH = 0xA8;
public const int VK_BROWSER_STOP = 0xA9;
public const int VK_BROWSER_SEARCH = 0xAA;
public const int VK_VOLUME_MUTE = 0xAD;
public const int VK_VOLUME_DOWN = 0xAE;
public const int VK_VOLUME_UP = 0xAF;
public const int VK_MEDIA_NEXT_TRACK = 0xB0;
public const int VK_MEDIA_PREV_TRACK = 0xB1;
public const int VK_MEDIA_STOP = 0xB2;
public const int VK_MEDIA_PLAY_PAUSE = 0xB3;
public const int KF_UP = 0x8000;
public const long KB_TRANSITION_FLAG = 0x80000000;
public const int VK_W = 0x57;
public delegate int HookProc(int code, int wparam, int lparam);
public int Proc(int code, int wparam, int lparam)
{
if (code == HC_ACTION)
{
switch (wparam)
{
case VK_BROWSER_BACK:
textBox1.Text += "Browser Back key caught" +
Environment.NewLine;
break;
case VK_BROWSER_FORWARD:
textBox1.Text += "Browser Forward key caught" +
Environment.NewLine;
break;
case VK_BROWSER_REFRESH:
textBox1.Text += "Browser Refresh key caught" +
Environment.NewLine;
break;
case VK_BROWSER_STOP:
textBox1.Text += "Browser Stop key caught" +
Environment.NewLine;
break;
case VK_BROWSER_SEARCH:
textBox1.Text += "Browser Search key caught" +
Environment.NewLine;
break;
case VK_VOLUME_MUTE:
textBox1.Text += "Volume Mute key caught" +
Environment.NewLine;
break;
case VK_VOLUME_DOWN:
textBox1.Text += "Volume Down key caught" +
Environment.NewLine;
break;
case VK_VOLUME_UP:
textBox1.Text += "Volume Up key caught" +
Environment.NewLine;
break;
case VK_MEDIA_NEXT_TRACK:
textBox1.Text += "Media Next Track key caught" +
Environment.NewLine;
break;
case VK_MEDIA_PREV_TRACK:
textBox1.Text += "Media Previous Track key caught" +
Environment.NewLine;
break;
case VK_MEDIA_STOP:
textBox1.Text += "Media Stop key caught" +
Environment.NewLine;
break;
case VK_MEDIA_PLAY_PAUSE:
textBox1.Text += "Media Play/Pause key caught" +
Environment.NewLine;
break;
}
}
return (CallNextHookEx(hookHandle, code, wparam, lparam));
}
private void Form1_Load(object sender, EventArgs e)
{
wmp.PlayStateChange +=
new WMPLib._WMPOCXEvents_PlayStateChangeEventHandler(Player_PlayStateChange);
wmp.MediaError +=
new WMPLib._WMPOCXEvents_MediaErrorEventHandler(Player_MediaError);
}
I don't get any errors, but it doesn't appear to work. Any ideas?
WHEELS
|
|
|
|
|
Can you please post your call to SetWindowsHookEx.
What hook are you using WH_KEYBOARD or WH_KEYBOARD_LL?
Because the function delegate called by the hook (Proc in your case)
can be called from threads/processes beyond your scope, you should not
do this:
textBox1.Text += "Browser Back key caught" + Environment.NewLine;
Use an invoke or for testing purposes just trace.
Greetings
Covean
|
|
|
|
|
Hi Covean.
That is all the "hook" code I have. I am not exactly clear what you are asking for. WHEELS
|
|
|
|
|
Ok so this will be the problem. ^^
Without calling SetWindowsHookEx how should the system know what you wanna hook?
Somewhere you should call (for example at form init):
IntPtr hHook = SetWindowsHookEx(WH_KEYBOARD or WH_KEYBOARD_LL(for low-level hook), Proc (delegate to your hook proc), Process.GetCurrentProcess().Handle, 0);
if(hHook == IntPtr.Zero)
{
// hook failed
}
At destruction time call UnhookWindowsHookEx(hHook);
Greetings
Covean
|
|
|
|
|
Now I understand. I have the following:
private void button1_Click(object sender, System.EventArgs e)
{
if (hookHandle == 0)
{
cb = new HookProc(Proc);
hookHandle = SetWindowsHookEx(WH_KEYBOARD, cb, 0,
appdomain.GetCurrentThreadId( ));
}
else
{
textBox1.Text += "Hook already set" + Environment.NewLine;
}
textBox1.Text += "Start: " + hookHandle + Environment.NewLine;
}
private void button2_Click(object sender, System.EventArgs e)
{
textBox1.Text += "End: " + UnhookWindowsHookEx(hookHandle) +
Environment.NewLine;
hookHandle = 0;
}
I am coming up with an error on this line:
hookHandle = SetWindowsHookEx(WH_KEYBOARD, cb, 0,
appdomain.GetCurrentThreadId( ));
appdomain doesn't exist in the current context.
WHEELS
|
|
|
|
|
If you want to hook more than the input to your application, you should set dwThreadId to 0 and just set the instance handle (3rd parameter) to Process.GetCurrentProcess().Handle.
Btw: appdomain does not exists its called AppDomain.
Greetings
Covean
|
|
|
|
|