Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
B

Bojan Rajkovic

@Bojan Rajkovic
About
Posts
17
Topics
4
Shares
0
Groups
0
Followers
0
Following
0

Posts

Recent Best Controversial

  • Minimize button event
    B Bojan Rajkovic

    Hook the Resized event of the form and then check that the WindowState is equal to FormWindowState.Minimized. If it is, issue your messagebox and do anything else that you want to.

    C# help question csharp

  • How to copy struct to byte [] ?
    B Bojan Rajkovic

    Well, to copy a struct to a byte[], you need some marshaling code..It uses the Marshal class (as if that wasn't obvious) to allocate some memory..Like this: (i usually declare methods like this as an instance method and a static method with a parameter of whatever type it's defined in)public byte[] ToBytes() { byte[] buff = new byte[Marshal.SizeOf(typeof(MyType))]; GCHandle handle = GCHandle.Alloc(buff, GCHandleType.Pinned); Marshal.StructureToPtr(this, handle.AddrOfPinnedObject(), false); handle.Free(); return buff; }
    That code will marshal the struct to a byte[], and from that code, the code for marshaling back to a struct comes easy:public static MyType FromBytes(byte[] buff) { GCHandle handle = GCHandle.Alloc(buff, GCHandleType.Pinned); MyType mt = (MyType)Marshal.PtrToStructure(handle.AddrOfPinnedObject(), typeof(MyType)); // because ptrtostructure returns an object :-\ handle.Free(); return mt; }
    Now as for chaining structures together, you can do that yourself using Array.Copy() and copying into a byte[] Passing back the data as an IntPtr requires only slight modification to the original functions:

    public IntPtr ToIntPtr()
    {
       byte[] buff = new byte[Marshal.SizeOf(typeof(MyType))];
       IntPtr handle = Marshal.AllocHGlobal(buff.Length);
       Marshal.StructureToPtr(this, handle, true); // passing false for that last param can cause memory leaks
       return handle;
    }
    
    public static MyType FromIntPtr(IntPtr ptr)
    {
       MyType mt = (MyType)Marshal.PtrToStructure(ptr, typeof(MyType)); // because ptrtostructure returns an object :-\
       //Marshal.FreeHGlobal(ptr); - only do this if you know you won't be using that IntPtr again
       return mt;
    }
    

    Note: That code is not 100% guaranteed to compile or work, I didn't test it very much, but it should work, and the basic concept is more or less correct. If you have any more questions, feel free to ask.

    C# csharp question data-structures json tutorial

  • how to zip and unzip folder through c#
    B Bojan Rajkovic

    Use #ziplib from ICSharpCode... http://www.icsharpcode.net/OpenSource/SharpZipLib/Default.aspx[^]

    C# csharp sysadmin tutorial

  • Icon from embedded resource
    B Bojan Rajkovic

    Get a stream to the resource using Resource.GetStream() and then use Image.FromStream()..You should be able to cast that to an icon or something to that effect...

    C# graphics hardware question learning

  • Resource assembly on CD
    B Bojan Rajkovic

    You say they're videos..If the routines you're using to play them are capable of taking byte[]'s, you could store the videos in an arbitrary binary format....What I mean is you can perhaps create a struct that holds a string describing the video and a byte[] that has the actual video data, then serialize that out to a binary file...Your customers would probably have a hard time getting anything out of that.

    C# csharp question learning

  • iphlpapi.dll and sendarp
    B Bojan Rajkovic

    I actually renamed the function when I was typing the comment (so as to simplify for you - I represented an IP as a long variable instead of an int in my app). :) Did it help you with what you needed?

    C# question csharp tutorial

  • iphlpapi.dll and sendarp
    B Bojan Rajkovic

    Mmm, no, that won't work.. You can't create integer IP's from just using Convert.ToDecimal(). That would be too simple. Also, you can't pass in a 0 for the 3rd parameter, it's a byte[] that's returned out as your data that you receive from the ARP I think (I haven't done any research, I'm just letting you know basic things that are wrong.) What this all means is that you need to: a) Declare a byte[] to use for the 3rd parameter. I'd reccomend using one that's got a decent length, like 256...declared as so: byte[] buffer = new byte[256]; (Note: You might not even need to declare a length, since you have that ref integer parameter that seems to tell you how much data you got out of the function - I'd try both ways) b) for the 4th parameter, you need to declare an integer before calling the function, then pass that integer in as a ref parameter...that variable will hold the length of the actual output from the function (how much data it filled in the buffer) c) you need IP's as integers...fortunately for you, I've written code that does that. :)

    public static int StringIPToInt(string IPString) 
    		{
    			string[] _splitString = IPString.Split(new char[]{'.'});
    			int _retVal = 0;
    			if (_splitString.Length < 4)
    				throw new ArgumentException("Please pass in a valid IP in dotted-quad notation!","IPString");
    			else 
    			{
    				_retVal += (int)(int.Parse(_splitString[3]) * Math.Pow(256,0));
    				_retVal += (int)(int.Parse(_splitString[2]) * Math.Pow(256,1));
    				_retVal += (int)(int.Parse(_splitString[1]) * Math.Pow(256,2));
    				_retVal += (int)(int.Parse(_splitString[0]) * Math.Pow(256,3));
    			}
    			_retVal = (int) (((_retVal & 0x000000ff) << 24) +
    				((_retVal & 0x0000ff00) << 8) +
    				((_retVal & 0x00ff0000) >> 8) +
    				((_retVal & 0xff000000) >> 24));
    			return _retVal;
    		}
    

    I won't go into detail except that all the compound addition you see is parsing the dotted-quad notation and creating an integer value to represent it (dotted-quad just stores the 32 bits of an IP address as four octets, so it's easy to go back to a 32bit integer). The last bit of code with all the bitshifting and binary AND's flips the endianness of the output integer so that you have the correct value. So, a call like this may be appropriate:

    byte[] outputData;
    int dataLength = 0;
    SendARP(StringIPToLong("192.168.1.101"),StringIPToLong("127.0.0.1"),outputData,ref dataLength);
    

    I would then use BitConverter.ToString() or something similar to get

    C# question csharp tutorial

  • Trouble with marshalling structure
    B Bojan Rajkovic

    What do you mean it's not being marshaled correctly? As in it won't compile? As in when you try to use the structure it behaves unexpectedly? Did you try using the MasrhalAs(UnmanagedType.R4) or MarshalAs(UnmanagedType.R8) properties? As I recall, a C double is smaller than a C# double or something to that effect. (The reason I think this is because for some reason I think that a C long is actually 4 bytes while a C# long is 8 bytes) So try adding a MarshalAs(UnmanagedType.R4) to your Double. (R4 and R8 marshal floating-point numbers, I2, I4, and I8 marshal integers)

    C# help csharp

  • Check Internet Port Status ?
    B Bojan Rajkovic

    The only thing I can think of is that you try to programatically open ports in a while loop which has a try-catch inside to try to open the socket, and catch the exception and continue the loop...You can also figure out which specific exception will fire when the port is in use and only catch that one, but let other exceptions be thrown or just catch-all and assume that it's an already open-port.

    C# tutorial sysadmin question

  • Update datagrid in a MDI child from a MDI child!!!URGENT HELP REQUIRED!!!
    B Bojan Rajkovic

    Uh...What you need to do is maybe set a timer in form 2 to refresh the data in the datagrid.. I have an app that I do something similar in, and all you need to do is call ReadResults() and do dataPreResult.Invalidate() (to make sure that it gets redrawn) every so often..I'm not 100% sure on that, but it should work. Here's the relevant code: **Timer updateDataGrid = new Timer();** // add this to your object declarations public FORM2() { InitializeComponent(); **updateDataGrid.Interval = 60*1000; updateDataGrid.Tick += new EventHandler(doUpdate); updateDataGrid.Start();** // add the above 3 lines in the constructor to set the interval, the handler for the timer firing and to start it. updating every minute seems like a good time, but you can change that } private void doUpdate(object sender, EventArgs e) { ReadResults(); dataPreResult.Invalidate(); }

    C# database graphics docker help announcement

  • ListBox owner drawing
    B Bojan Rajkovic

    Can anyone point me to a good way to draw a usercontrol inside a listbox over and over again? What I mean is to insert something like a panel (with stuff on it obviously) inside a listBox and have that be displayed instead of some random strings (I've tried inserting it and I get random strings...I honestly don't remember what though). I REALLY don't wanna have to draw everything with GDI+ so if anyone knows a way to do that, I would appreciate it greatly. Note: It can also be a listview - either will do fine. I haven't found anything on CP that shows how to do that, so if there is something, please point me there. and forgive my stupidity.

    C# graphics winforms tutorial question lounge

  • Loading an assembly dynamically and using types/functions in it
    B Bojan Rajkovic

    I'm trying to load an assembly dynamically (IE. extracting it as an embedded resource, and then using Assembly.Load() to load it) and then use the types and functions that are built into it, but I can't figure out how to get access to them, nor could I find any good tutorials. Any hints, or am I trying the impossible?

    C# hardware tutorial question learning

  • time
    B Bojan Rajkovic

    DateTime.Now.ToLongTimeString() or something like that. I'm too damn lazy to look it up.

    C# question

  • Deleting a listbox item
    B Bojan Rajkovic

    Well, it would be quite easy to remove the item using listBox1.Items.Remove(listBox1.SelectedItem) or something similar I believe. And after you have done that, why not just call a method that will rewrite your text file with the current listbox items..That would be the simplest approach in my opinion.

    C# csharp question learning

  • Calling a function in another window.
    B Bojan Rajkovic

    I'm writing an MP3 player, with queueing capabilities - it writes the queue to a temporary text file, and I also have another form in the app that allows me to view the queue. This works fine, but I can't refresh the queue automatically. I would like to be able to just call the writeQ function in the main form to write the queue, then call the queue viewer forms' load function which would read the necessary data. But, my problem is, I don't understand how I can get the handle of the main form to call the writeQ. simply doing a MainPlayer mainP = new MainPlayer() then calling mainP.writeQ() (MainPlayer is the main form class) won't work, because it'll try to read the listBox in the queue viewer form...Anyone know how I could force it to read the listBox in its own form? I would think I can do it using IntPtr and window handles, but I have no idea quite how to do this.

    C# data-structures help tutorial question

  • A little help..
    B Bojan Rajkovic

    I tried using nBass..It really never worked for me. No matter what, I couldn't get files to play...DX9 works fine for now. About the error, I'll get back to you about it as soon as I reproduce it. I'm writing this right before I get ready for a shower and some well-deserved rest, so that'll most likely be tomorrow. (I know, I should've posted it, but my mind slipped)

    C# help graphics game-dev question

  • A little help..
    B Bojan Rajkovic

    I'm writing an application at the time, an MP3 player (using DX9's AudioVideoPlayback class). I've got it fairly full-featured: ID3v1 and v2 tag reading, writing will come in the future (I use UltraID3Lib for this); playlists; shuffle and repeat, etc. I've got two small problems though: My first one is that whenever I try to set the Volume property of the DirectX.AudioVideoPlayback.Audio class, it throws an error. Anyone know why? My second invovles the playlist: it's a regular listbox, which I simply feed with filenames (with full paths, so that the audio class can know what to open) - I would like to be able to display ID3 tags here though - I was wondering if anyone knew an easy way for me to display ID3 tags, but have a hidden field of some sort that holds the path for that particular item. I don't know if I could maybe do this with a listView or not, but if anyone knows, please let me know. If it has to be done using owner drawn controls, I would really appreciate if someone wrote the code, they would receive full credit for it. Thanks in advance.

    C# help graphics game-dev question
  • Login

  • Don't have an account? Register

  • Login or register to search.
  • First post
    Last post
0
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups