|
The function in unmanaged DLL get char* as parametar. When I try to use that function with (out) string parametar, program breaks. How to use that function?
|
|
|
|
|
A char* is a string - they're both already pointers. So, don't use out or ref with a string unless you see an unmanaged declaration like char** or LPCSTR* or something.
BTW, if the string is actually a TCHAR* , TCHAR[] , or LP(C)TSTR , then make sure you specify CharSet.Auto . If the declaration is a char* or LP(C)STR , be sure to specify CharSet.Ansi since strings are Unicode in .NET natively. Both the DllImportAttribute and StructLayoutAttribute define a CharSet field you should set in the attribute in such a case.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
If I pass only string as parametar program breaks with error message: "Object reference is not set to an instance of an object". Pass parremetar is char* type in dll.
|
|
|
|
|
And is the string set to something? Trust me, a char* is a string - in any language.
If the string is supposed to be an output parameter, do not use the out keyword but instead use the OutAttribute . This tells the marshaler that the parameter is an output parameter and will be assigned when the function returns.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
If I understand you well, this is a solution for problem:
[DllImport("Kernel32.dll", CharSet=CharSet.Ansi)]
public static extern bool GetComputerName([Out,MarshalAs(UnmanagedType.LPTStr)] string f1, out int a2);
.
.
.
int v = 0;
string a = "";
if (GetComputerName(a, out v))
{
MessageBox.Show(a+" " + v);
}
But, this will not work???
|
|
|
|
|
You have that all wrong. The character set should be CharSet.Auto (since, like I said before, the string is LPTSTR ), and the second parameter is documented as [in, out], which means you should use ref because you MUST pass the size of the string buffer, which is probably why you're getting the error (not to mention the wrong character set when using Windows NT (4.0, 2000, XP, 2003). The property signature is:
[DllImport("user32.dll", CharSet=CharSet.Auto)]
public static extern bool GetComputerName(string name, ref uint size);
uint size = 16;
string name = new string('\0', (int)size);
GetComputerName(name, ref size); You know what, though? You should browse the class library documentation and learn what's there. This P/Invocation is not necessary. Just use the Environment.MachineName property.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Why can't you remove it? How are you trying to remove it? Please be more specific or we really can't help you with your problem.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
its because i have no idea how to remove it!
i tried...
i have a xml document called Data!
string id = listBoxSpil.SelectedItem.ToString().Substring(0,2);
//the value of id is 1
XmlNode remove = Data.SelectSingleNode("//food[@id='" + id + "']")
Data.RemoveChild(remove);
But it doesn't work
so if you can tell me a way to remove it please help me!
|
|
|
|
|
First of all, make sure the id string is correct. When you call RemoveChild , you must call it on the XmlNode.ParentNode , because the XmlNode will most likely not be a child of the document (which only has one child - the root node, a.k.a. document element.
So, change Data.RemoveChild(remove ) to remove.ParentNode.RemoveChild(remove) and it should work.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
heyY thanks man... it work now!!!
if there is anything i can do... just say it!
|
|
|
|
|
... But don't call me Sherly. I have a VS.NET 2002 project that uses a 1.1 Framework dll and it is giving me the StrongNameIdentityPermission error because it is looking at the MachineConfig for the 1.0 Framework. How do I get my project to the look at the 1.1 Framework Machine.Config?
|
|
|
|
|
You mean you have an assembly compiled with VS.NET 2002? VS.NET 2002 will only work with the .NET Framework 1.0.
In order to read from the machine.config file you want, mscoree.dll must load the appropriate CLR. This is determined by the assembly publishing policy or the runtime configuration in the .config file. This will only work at runtime however.
Read about the Runtime Settings Schema[^] in the .NET Framework SDK, as well as Redirecting Assembly Versions[^], which may or may not apply depending on what you're trying to do.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
I am using the Assembly.LoadFrom() method in my application to extract some information from some DLLs and I am running into a locking problem. Even after I am done with the file and have set the Assembly to null it keeps the file I loaded locked until the application terminates. The problem comes when I try and update the DLL with a new version automatically via the main application. I am unable to copy the newly downloaded version over because the file still reports as locked by Windows.
Does anyone know a way to tell the Assembly object to release it's hold on the file it loaded?
|
|
|
|
|
An assembly cannot be unloaded from an AppDomain . In order to make sure the file doesn't remained locked in use, however, you can read-in the file, grab the raw bytes, and load the buffer:
byte[] assembly;
using (FileStream fs = new FileStream(pathToAssembly, FileMode.Open,
FileAccess.Read, FileShare.Read))
assembly = new byte[fs.Length];
fs.Read(assembly, 0, assembly.Length);
fs.Close();
}
AppDomain.CurrentDomain.Load(assembly, null);
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Thanks, that method works great.
|
|
|
|
|
I have implemented a code that runs at a Pocket PC 2002 platform. This is the client part , no problem. It saves some data such as names and adress information in XML document. The question is i want to send this data to a PC and upload necessary things. But i dont know how to start. the thing is just simple to upload XML file from PDA to PC or visa-versa. I couldn't find an article about this staff.if any one can tell me how to that or advises some article or site I will be glad. Thank you.
|
|
|
|
|
Hey Guys,
Here's the problem: I'm trying to learn multi-tier design so I've been writing a little app to teach myself. I have a normalised SQLServer database which contains my music collection. The DB schema is here[^].
So, I used LLBLGen[^] to generate a C# class for each of my table entities and that works great. There are SelectOne() and SelectAll() methods on each of my entities which allow you to select one record or the whole table.
My question is: how do I join the tables together? And how do I search them? I tried asking on the asp.net forums but they just suggested buying a OR mapper tool.
Cheers
James
|
|
|
|
|
It will be kind of difficult using the schema you have defined. Where are your foreign keys (FKs)? The full join should look something like this (assuming proper FK development):
SELECT *
FROM Artists
INNER JOIN Albums ON (Artists.ArtistId = Albums.ArtistId)
INNER JOIN Tracks ON (Albums.AlbumId = Tracks.AlbumId)
INNER JOIN Files ON (Tracks.TrackId = Files.TrackId)
The type of joins you need to develop will depend on how you want the data displayed.
|
|
|
|
|
Thanks for the reply Michael.
I know how to do the JOIN in sql, that's not the problem. The problem is that from a design point of view each table is a separate C# class upon which I call the CRUD methods (album.Delete(), album.SelectOne() etc). What I need to know is what is the best way of joining the tables under this scenario?
I've been think of defining views for each JOIN which I require and then generating classes for each of those but I'm not convinced it is the best way to go about it.
Cheers
James
|
|
|
|
|
Hi,
I have developed a plugin for Excel that i need to test. I need to simulate the user entering a lot of data into the sheet and was wondering if there was an app./plugin out there that does that.
Any help is appreciated.
Thanks.
|
|
|
|
|
I need to load a datagrid on a tabpage then if a user moves form tab to tab i want to retain the values they may have added but as there is no Load event for a tabpage im stuck, the enter event is no good as it would reset the datagrid values back to defauls? So when the tabpage loads i want to display default values, then if the user alters them and move yto another page and back again the values remain. No update to the database have been executed.
|
|
|
|
|
The TabControl which contains the TabPages has an event SelectedIndexChanged , it is called before the next TabPage is shown. You can use it instead of a TabPage.Load event.
|
|
|
|
|
that would still cause the values to reset?
|
|
|
|
|
I am applying 2 different grid styles to the same datagrid. When I apply first style to the grid it gets applied to the grid, but when i apply the second style, it does not get applied. I am clearing the grid style before applying the new style.
datagrid1.TableStyles.Clear();
Can anybody tell me what is the reason for this and what is that I am missing??
Thanks in advance.
|
|
|
|
|
If the DataGridTableStyle.MappingName is different for both (and it must match up with the data source, such as a DataTable , array, or an IList implementation's class name), then you don't need to clear them. That's why the DataGrid.TableStyles property is a collection property. So long as the styles handle different data sources, it's not a problem to keep both in there. In fact, I'm pretty sure you're using different MappingName s since the Add method checks for duplicates and throws an exception in such a case. Just keep both styles in the collection.
When you're applying a style, you must re-bind the data source. The data source is "stylized" during binding.
Microsoft MVP, Visual C#
My Articles
|
|
|
|