Tuesday, February 10, 2009

A problem of forward declaration in C++/CLI

Today while I started debugging my program, the programmed stopped at
Assembly assembly=Assembly.Load(name)
foreach(Type type in assembly.GetTypes()
{
...
}
and the output window showed:
First-chance exception at 0x7c81eb33 (kernel32.dll) in myprogramm.exe: Microsoft C++ exception: EETypeLoadException at memory location 0x0012d324..
First-chance exception at 0x7c81eb33 (kernel32.dll) in myprogramm.exe: Microsoft C++ exception: [rethrow] at memory location 0x00000000..
A first chance exception of type 'System.Reflection.ReflectionTypeLoadException' occurred in mscorlib.dll
Later I found that all these happens due to a unresolved forward declaration of a ref class. but the unresolved native class will not cause the problem.

So be caution when you are using the forward declaration for a ref class.

Tuesday, January 13, 2009

Create console for nonconsole application.

First we need to create a console:
BOOL result=AllocConsole();
assert(result);
Then we need to get the handle to the console, and associate the console to the standard output stream:
HANDLE hOutput=CreateFile(L"CONOUT$", GENERIC_WRITE, FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
SetStdHandle(STD_OUTPUT_HANDLE,hOutput);
In CRT, we also need make the stdout usable:
#include
#include
int hCrt=_open_osfhandle((intptr_t)hOutput,_O_TEXT);
FILE* hf=_fdopen(hCrt,"w");
*stdout=*hf
If in C++/CLI, We also need to replace System::Console::Out:
ref class __ConsoleWriter : public TextWriter
{
private:
HANDLE handle;
__ConsoleWriter(HANDLE handle):handle(handle){}
public:
virtual property System::Text::Encoding^ Encoding
{
System::Text::Encoding^ get()override
{
return System::Text::Encoding::UTF8;
}
}
virtual void Write(array^ buffer, int index, int count)override
{
array^ bytes=Encoding->GetBytes(buffer, index, count);
pin_ptr p=&bytes[0];
DWORD n;
WriteFile(handle, (LPVOID)p, bytes->Length, &n, NULL);
}
};
//Redirect the CLR output
Console::SetOut(gcnew __ConsoleWriter(hOutput));

That's it, quite simple.

Monday, December 08, 2008

The buggy .net framework 2.0

I've been using the .net framework for years, my work didn't involved with the UI development for the most time, right now I'm developing a game editor for our game, in the months developing, I found some problems about the framework, and I'm sure they are bugs, I didn't tried them on .net framework 3.5, I don't know if these bugs have been resolved.

1st bug, the framework handles the shortcut keys incorrectly.
Associate a context menu strip to a control like TextBox, or place a MainMenuStrip on the form will take the same effect, make sure there is a menu item which have a shortcut key that could get conflicted with the textbox, like a Delete key. ok, run the program and press the key like Delete, you will find the key takes no effects in the control, this is a big problem that can not be ignored, you never need a standard text box that can only use Backspace but not the Delete key(if you have a shortcut with Backspace, you will only able to remove characters by using mouse's context menu), this problem quite sucks.
Hack Solution: Uses reflection to retrieve the FieldInfo shortcuts in type ToolStrip, overrides the control or the form's ProcessCmdKey, use the FieldInfo to access the private field of the context menu strip or main menu strip, it is a System.Collections.HashTable, if the hash table contains the shortcut(you need cast the wParam to type Keys), resend the message to the control that you wish to recieve the message.


2nd bug, the ScrollableControl resets the scrollbars' position when they get the focus again.
This bug happens if a ScrollableControl like Panel contains a child control which size is larger than the container and the ScrollableControl have AutoScroll assigned to true. If the ScrollbarControl have the none zero scrollbar's position, either the horizontal scrollbar or the vertical scrollbar, when it gains the focus, both the horizontal and vertical scrollbar's position will reset to zero.
Hack Solution: A really damn shit way to solve this, back up the AutoScrollPosition value of the ScrollableControl when the child control lost focus, when the child control gains the focus again, multiply the X and Y of the backed up value with -1, assign the result to the AutoScrollPosition.

Wednesday, July 18, 2007

v1.4.3 ogre-based application failed to execute

Windows doesn't provide any useful information, and the debugger only told one:
LDR: LdrpWalkImportDescriptor() failed to probe for its manifest, ntstatus 0xc0150002

This is a very rusty problem, later I found how to solve it.
The v1.4.3 ogre sdk require VS2005 SP1, you only need deploy your system with a directory, <vs>\vc\redist\x86\Microsoft.VC80.CRT
now the dialog will no longer pop up. enjoy your ogre time!

if another error dialog about directx poped up, just update to the newest directx redist.

Thursday, February 15, 2007

China's New Year

It says that in the ancient age, there was a monster call Nian(or Year), with two acuminate horns, very ferocious, it lives in deep see, only crawl to the land every last night of the year, hurt people and animals. So every in that night, all the villagers will hide inside the mountains, to get away from Nian's hurt.
One last night of the year, villagers were preparing to leave like every years before, here comes a old man with all white hairs, he told a old woman, if she allowed to let him stay one night in her house, he can definitely can drive Nian away, but nobody give him a shit, they left as usual, the old man insisted to stay.
When the Nian rushed into the village that night, suddenly the old man fired firecrackers, Nian got frighten, and dare not to move forward even one step, Nian has fear with red color, fire flash and explodents. One gate opened, inside the yard, a old man in red laughed in a big voice, Nian left as much as it could while it saw the old man.
In the second day, villagers returned to the village, they found everything are still what they was. So they knew the old man with white hair is a god, he came to help them to drive Nian away, and they knew the three issues to drive Nian away. From that day on, in each last night of the year, every home stick red papers with nice words on them beside their gates, and every home fire firecrackers, keep light for a whole night, wait to the next year's first day.
This custom go round, now it becames china traditional most important and most ceremonious festival. And this day we called in chinese "Guo Nian", means the Nian past(Year past).

Wednesday, February 14, 2007

ASP.NET 2.0 returns a HTTP 404 Error

ASP.NET 2.0 support a new feature, when you need to maintaince your web site, you can put a empty file named app_offline.htm to the site's folder, if this file's size is less than 512 bytes, all request will get a HTTP 404 error, or redirect to app_offline.htm.

I met a problem, I didn't have such a app_offline.htm file, but I always get a http 404 error under .net 2.0(Server OS: Win2003), later i know, the IIS6 prohibited .net 2.0, if you want to reenable it, you need to run "aspnet_regiis -i -enable" under C:\windows\Microsoft.NET\Framework\v2.0.50727, this simple trick spend me hours to solve
#- -

Today is Valentine's Day, I'm single, I have nothing to do except doing my work, four days later, that will be Chinese traditional festival, Sprint Festival, all of us are busy preparing for that.

Tonight I want to write something about chinese new year.

Sunday, January 21, 2007

Dog's Dying Crying

From last year july, I heard the news there's a county launched dog slaughtering, they did it just only because the rabies let some people dead! But just heard, until today.

Why dog bites? if you do not show your animus to a dog, if you do not attack a dog, dare it bite you? No! Those hydrophobic dogs already died or sent to animal salvation station because of they might hurt people. The rest dogs they are smart, even if a human kind, will not attack a bigger animal, dare you attack a elephant or lion? Keep self safe is the basic law in nature, whatever a human being or an insect, every creature obey this law.

The government can have another better way to prevent rabies, they can set up better welfare system, let the best friends of human can have a place to live. Or promote people's moral diathesis, why I say moral? Look at the original place where starts dog slaughtering, that's a county where the people's average moral diathesis is quite low, maybe someone out there wants to relieve their discontentment, so they attack dogs. Why the government choose dog slaughtering? If they corrupt a little more, the saved money is absolute enough to build pet welfare system. How can we continuely believe the government's intelligence? Human kind developed millions years, why there are so many people's intelligence is still in a lower level?

Just less than a century before, in the second world war, no one will forget those massacres on human, the racial massacres will be shameful wounds and stay in history forever, nanjing, ukraine, italian massacres, who can remember? In today, there are more massacres in Africa. What's inside human? the evil never ends.

We kill mosquitos because of the yellow fever, We kill birds because of the avian influenza, We kill civets because of SARS, We kill dogs because of rabies, when shall we start to kill human beings because of AIDS/lunacy/hepatitis/etc? Human body can take far more contagious diseases than any other creatures in the world can do. We Kill, Kill, Kill, Until nothing can be killed except ourself.

Last month, I saw a dog, and it scared not because of my look, but of I'm a human! It might just get away from those slaughterman. Today, I saw a dog dying by human's selfish, this dog, looks familiar.