AMcTools Forum 
  HomeHelpSearchLogin  
 
Pages: 1 2 3 
Send Topic Print
Problem compiling userlib dll (Read 25465 times)
Gif
YaBB Newbies
*
Offline

I Love YaBB 2!

Posts: 12
CzechRepublic
Gender: male
Problem compiling userlib dll
12/29/09 at 19:19:00
 
When I trying make own user component, I can't compile it by MS Visual C++ 2005. Even with static-linked stdlib, it in VMlab print:

Located Callback Functions:
   On_simulation_end( )
   ... other functions
   On_window_init( )
Corrupted DLL: reason = 4
* d:\test\test.prj, line 33: Bad X instance. Check parameters


When I compile same source with Borland, it can be used in VMlab without problems.
What can I do to compile by VC?
Thanks
Back to top
 
 
  IP Logged
moderator
YaBB Administrator
*****
Offline

I love forums, don't
you?

Posts: 308
AMcTools (Spain)
Gender: male
Re: Problem compiling userlib dll
Reply #1 - 12/30/09 at 09:06:27
 
This error is due to an alignement problem in strings.

See the default options used in the Borland compiler and use the same ones in VC++. Insure also that the char variable is not wide_char (Unicode).

If you find out the solution, please, let me know in order to write the corresponding advice in the documentation, otherwise post the DLL to analyze.





Back to top
 
 
WWW   IP Logged
ThVortex
Global Moderator
*****
Offline



Posts: 160
Austin, TX, USA
Gender: male
Re: Problem compiling userlib dll
Reply #2 - 12/30/09 at 10:14:58
 
Another possibility. Look for any compiler options like "Merge duplicate strings" (or "string literals"). Make sure this option is disabled so duplicate strings are [i]not[/i] merged. If you can't find any such option, then try to enable an option that allows string literals to be writable.
Back to top
 
 
WWW   IP Logged
Gif
YaBB Newbies
*
Offline

I Love YaBB 2!

Posts: 12
CzechRepublic
Gender: male
Re: Problem compiling userlib dll
Reply #3 - 12/30/09 at 12:43:24
 
I have no luck.
No-unicode and no-merge strings I have already before. The no-merge strings I mean "Enable String Pooling = No".

What exactly do you mean by "alignement problem in strings"?
I have no direct strings in code nor have resources. But there are some strings in pins declare macros.

In attachment is dll, both with staticaly and dynamicaly linked stdc by VC, and also by BCC.



Btw: In "usercomp mini ide" I used macros in "", because have bcc instaled in directory with space (C:\Program Files\bcc55\)
So use:
bcc32 -I"$Include"  -c  -w-par  -R  -H -WM-  -vi  -WD $Name.cpp
ilink32 -L"$Lib; $Lib\psdk" -Tpd -aa -x -C "$Name.obj" $Lib\c0d32.obj", , , "$Lib\import32.lib"  "$Lib\cw32.lib", ,  
Back to top
 
  IP Logged
moderator
YaBB Administrator
*****
Offline

I love forums, don't
you?

Posts: 308
AMcTools (Spain)
Gender: male
Re: Problem compiling userlib dll
Reply #4 - 12/30/09 at 15:12:04
 
Thanks, I have run your example with the debugger; the cause is this:

If you analyze the blackbox.h, the pin declarations block produces an area of code more or less, like this:

const char *pinDeclBegin = "{"

const char *pin1Name = "PIN1";
const char *pin1Type = "I";
const char *pin1Index = "1";

.... same stuff for each pin

const char *pinDeclEnd = "}";

VMLAB assumes (maybe a too strong assumption...!) , that the compiler will alocate such static strings one after the other in a growing address basis, like

"{"
"PIN1"
"I"
.....

"}"

And then, this stuff is parsed to get the pin types, etc....

But before this, VMLAB performs some consistency checks to avoid problems, for example, that the address of "{" is less than the one of "}". And here comes the problem.

Your VC++ compiler seems not  behave like this; it places the strings using some other criteria, or who knows... and so the "corrupted DLL" message.

Moreover, I have made a binary dump of both (Borland and VCC) DLLs, with the following result:

In Borland's is clearly visible the area where the strings are placed; indeed, I can see your pin names: (copy-pasted from dump)

{.@CLK.@1.@DI.@MISO.@2.@DO.@MOSI.@3.@DI.@CS.@4.@DI.@CD.@5.@DO.}

... so you are working in some SPI stuff....

Whereas if I make a dump of the VC DLL I simply see nothing readable! , even I cannot find any "CLK" nor "MOSI", etc.... are they encrypted??

So, let's go with the solutions:

- Quick/obvious: use Borland
- Question: maybe VC does not understand well the macros..? extrange...
- See if there is some compiler option controlling this static string allocation sequence; not sure about....
Back to top
 
 
WWW   IP Logged
ThVortex
Global Moderator
*****
Offline



Posts: 160
Austin, TX, USA
Gender: male
Re: Problem compiling userlib dll
Reply #5 - 12/30/09 at 17:44:32
 
Have you tried compiling the DLL in "Debug" mode so the optimizer is disabled? The string variables created by the pin declarations block are never directly referenced so I wonder if the compiler decided the string literals are not needed.

In the meantime, I'll think about how we can re-write those pin declaration macros in blackbox.h so they aren't compiler dependent.
Back to top
 
 
WWW   IP Logged
moderator
YaBB Administrator
*****
Offline

I love forums, don't
you?

Posts: 308
AMcTools (Spain)
Gender: male
Re: Problem compiling userlib dll
Reply #6 - 12/30/09 at 18:30:27
 
Yeah, good point!

If problems, try a binary dump after compiling. A good DLL must show sometihng like the mentioned in the previous post.

In any case, we have identified several things wich can cause trouble, so it will be addressed in the documentation:

- wide char literals (Unicode).
- duplicated strings merge.
- removal of unreferenced strings by optimization

Back to top
 
 
WWW   IP Logged
Gif
YaBB Newbies
*
Offline

I Love YaBB 2!

Posts: 12
CzechRepublic
Gender: male
Re: Problem compiling userlib dll
Reply #7 - 12/30/09 at 18:36:12
 
Ok, I had no clue looking into binary before, so tried to make strings in same order in binary.
Now have "Corrupted DLL: reason = 5" :-)

I must set option "Keep Unreferenced Data (/OPT:NOREF)"  and reordered the strings manualy.

Please do you have time to try dll again?
I slowly loosing idea to make new component, but probably I'm not only who like compile with vc++. The main reason is possibility to debug the dll then.
Back to top
 
  IP Logged
Gif
YaBB Newbies
*
Offline

I Love YaBB 2!

Posts: 12
CzechRepublic
Gender: male
Re: Problem compiling userlib dll
Reply #8 - 12/30/09 at 18:45:30
 
Tried debug configuration also before, but removing unreferenced is there also default on.

And reoder strings manualy I must because order is exactly opposite - last string is first in binary. (don't know why)
Back to top
 
 
  IP Logged
moderator
YaBB Administrator
*****
Offline

I love forums, don't
you?

Posts: 308
AMcTools (Spain)
Gender: male
Re: Problem compiling userlib dll
Reply #9 - 12/30/09 at 19:59:46
 
OK, things are improving, you have increased the reason # [smiley=laugh.gif]

But your DLL dump still shows a extrange thing:

{...@CLK....@1..@MISO...@2..@MOSI...@3..@CS.@4..@DI.@CD.@5..@DO.}

Now the error comes from the parser phase, it seems that you have still a problem, the pin type identifiers DI, DO, etc, are missing

The whole string (you can see it with any dump utility) must be like this:

{.@CLK.@1.@DI.@MISO.@2.@DO.@MOSI.@3.@DI.@CS.@4.@DI.@CD.@5.@DO.}

Sequence: {Pin_name1, index1, pinType1, Pin_name2, ....}

What do you mean by "reorder pins manually"?
Back to top
 
 
WWW   IP Logged
ThVortex
Global Moderator
*****
Offline



Posts: 160
Austin, TX, USA
Gender: male
Re: Problem compiling userlib dll
Reply #10 - 12/31/09 at 09:13:25
 
Ok, I think I have a solution to the problem. I'm attaching a blackbox.h file that should work with any compiler. Instead of relying on the compiler's data layout, I wrote some code to create the proper string buffer at runtime.
Back to top
 
WWW   IP Logged
moderator
YaBB Administrator
*****
Offline

I love forums, don't
you?

Posts: 308
AMcTools (Spain)
Gender: male
Re: Problem compiling userlib dll
Reply #11 - 01/04/10 at 10:22:14
 
Great!  

I had thougt in reversing the pins parser if "{" address is higher than "}", but this solution, indeed is better.

So, I keep the last blackbox.h.

Of course, we have to make sure that all works fine in MS compilers
Back to top
 
 
WWW   IP Logged
moderator
YaBB Administrator
*****
Offline

I love forums, don't
you?

Posts: 308
AMcTools (Spain)
Gender: male
Re: Problem compiling userlib dll
Reply #12 - 01/04/10 at 10:26:41
 
i forgot...

For "Gif": please, give us feedback about if you have been able finally to compile your stuff wiht MS tools.
Back to top
 
 
WWW   IP Logged
Gif
YaBB Newbies
*
Offline

I Love YaBB 2!

Posts: 12
CzechRepublic
Gender: male
Re: Problem compiling userlib dll
Reply #13 - 01/04/10 at 20:19:01
 
Thanks for your file.
Sorry for later responce and now I have different compiler and different source (on different computer)

With your blackbox.h it works with VC++ (but now i tried it on VC++ 2008 Express), I only disable unicode strings.


I only lost some time and good mood with minor bug - if I change type of pin, vmlab doesn't respect new settings with recompiled dll.
For me it behave like still have Undefined value on output pin (which was bidir before). It takes me lot of tries as I try to rollback all changes and it still not work ... It works again after call SetDrive even on output pis (for which got error message) and close and reopen scope view. Then it still works after remove SetDrive again.
I don't try to reproduce it, so maybe there is some else conditions.
Back to top
 
 
  IP Logged
ThVortex
Global Moderator
*****
Offline



Posts: 160
Austin, TX, USA
Gender: male
Re: Problem compiling userlib dll
Reply #14 - 01/05/10 at 05:18:03
 
I downloaded VC++ 2008 Express myself, and I verified that with the new blackbox.h I could compile my I2C EEPROM component. I did notice some differences that I'd like to point out here (besides disabling Unicode).

First of all, Borland uses "DllEntryPoint" while Microsoft uses "DllMain". Most people shouldn't care about this but in case you actually rely on this function to save the HINSTANCE or something, then you should know. We could handle this in blackbox.h with a conditional "#define DllEntryPoint DllMain" when compiling with VC.

The resource compilers also have some differences. Given:
[tt]
STRINGTABLE
{
  1234, "foo\
bar"
}
[/tt]

Microsoft will produce "foobar" while Borland will produce "foo\nbar". Borland also allows \" as a quote escape in the string while Microsoft does not; thankfully, both resource compilers allow "" as a quote escape.

The Borland resource compiler has buggy VERSIONINFO support. I had to embed an explicit terminating NULL in my strings to keep it from corrupting the strings. I suspect there is a 2 byte alignment requirement in this resource that Borland ignores because it only breaks if the string has an even-number of characters. The extra NULLs cause no harm with Microsoft's resource compiler. Here's what my VERSIONINFO looks like:
[tt]
1 VERSIONINFO
FILEVERSION       0,1,0,0
PRODUCTVERSION    3,15,2,0
FILEOS            VOS__WINDOWS32
FILETYPE          VFT_DLL
{
  BLOCK "StringFileInfo"
  {
     BLOCK "040904E4"
     {
        VALUE "CompanyName",      "Wojciech Stryjewski\0"
        VALUE "FileDescription",  "I2C 24xxx Serial EEPROM\0"
        VALUE "FileVersion",      "v0.1\0"
        VALUE "InternalName",     "EEPROM24\0"
        VALUE "LegalCopyright",   "2009-2010, GNU LGPL v2.1+\0"
        VALUE "OriginalFilename", "eeprom24.dll\0"
        VALUE "ProductName",      "VMLAB\0"
        VALUE "ProductVersion",   "3.15B\0"
     }
  }
  BLOCK "VarFileInfo" { VALUE "Translation", 0x0409 0x04E4 }
}
[/tt]
Back to top
 
 
WWW   IP Logged
Pages: 1 2 3 
Send Topic Print