Thread overview
extern help.
Dec 26, 2005
Zz
Dec 27, 2005
James Dunne
Dec 28, 2005
Zz
Dec 27, 2005
John C
Dec 28, 2005
Zz
Dec 28, 2005
John C
Dec 29, 2005
Zz
December 26, 2005
Hi,

I have the following in a c header file. (the macros have been expanded).

typedef void(__cdecl *xmlFreeFunc)(void *mem);
__declspec(dllimport) extern xmlFreeFunc xmlFree;

how would I represent this in extern (C) and extern (Windows), I tried a few
thing but I allways get
Error: Access Violation
when executing xmlFree.

I'm sure, I'm getting something wrong.
in C its used as xmlFree(whatever);
in C++ I use xmlFree((void*)whatever);

Zz


December 27, 2005
Zz wrote:
> Hi,
> 
> I have the following in a c header file. (the macros have been expanded).
> 
> typedef void(__cdecl *xmlFreeFunc)(void *mem);
> __declspec(dllimport) extern xmlFreeFunc xmlFree;
> 
> how would I represent this in extern (C) and extern (Windows), I tried a few thing but I allways get
> Error: Access Violation
> when executing xmlFree.
> 
> I'm sure, I'm getting something wrong.
> in C its used as xmlFree(whatever);
> in C++ I use xmlFree((void*)whatever);
> 
> Zz 
> 
> 

Well, to me that looks like a DLLIMPORTed C function pointer (callback).  Best way to get to that in D (if you haven't already tried this) would be:

extern (C) typedef void function(void *mem) xmlFreeFunc;
extern (C) xmlFreeFunc	xmlFree;

Note that I'm not sure the effect of extern (C) on the typedef function pointer, nor on the extern'd variable there.  Someone clarify this?

I gather you're trying to hook up to an external XML parsing/validating library, something like MSXML.  Is it a LIB or DLL file?  Evidence from your post suggests to me that you should have a LIB, otherwise you wouldn't be extern-ing a variable called 'xmlFree'.  This is why you're getting an Access Violation - you're trying to jump to an address that isn't holding valid code and hasn't been initialized yet.

If you have a LIB file, then make sure it's in the OMF format (not the MS "standard" COFF format) and specify its filename on the DMD compiler's commandline when compiling your application.  I hear you can get a COFF-to-OMF conversion utility from the Digital Mars utility CD.

If a DLL file, then you'll have to assign the value of xmlFree to a valid function pointer.  Have a look at the Derelict project on dsource.org to get a feel for dynamic symbol loading from DLLs on Windows systems.
December 27, 2005
"Zz" <junkie@noware.com> wrote in message news:dopg4j$3jp$1@digitaldaemon.com...
> Hi,
>
> I have the following in a c header file. (the macros have been expanded).
>
> typedef void(__cdecl *xmlFreeFunc)(void *mem);
> __declspec(dllimport) extern xmlFreeFunc xmlFree;

The D translation is fairly straightforward:

    pragma(lib, "libxml2.lib");

    extern(C) alias void function(void* mem) xmlFreeFunc;
    extern(C) void xmlFree(void* mem);

Although http://www.digitalmars.com/d/windows.html says that __declspec(dllimport) should become 'export'. So if the above fails (although it worked for me with the Windows binaries from http://www.zlatkovic.com/libxml.en.html), try this:

    export void xmlFree(void* mem);

>
> how would I represent this in extern (C) and extern (Windows), I tried a
> few thing but I allways get
> Error: Access Violation
> when executing xmlFree.
>
> I'm sure, I'm getting something wrong.
> in C its used as xmlFree(whatever);
> in C++ I use xmlFree((void*)whatever);
>
> Zz
> 


December 28, 2005
"James Dunne" <james.jdunne@gmail.com> wrote in message news:doq20f$1427$1@digitaldaemon.com...
> Zz wrote:
>> Hi,
>>
>> I have the following in a c header file. (the macros have been expanded).
>>
>> typedef void(__cdecl *xmlFreeFunc)(void *mem);
>> __declspec(dllimport) extern xmlFreeFunc xmlFree;
>>
>> how would I represent this in extern (C) and extern (Windows), I tried a
>> few thing but I allways get
>> Error: Access Violation
>> when executing xmlFree.
>>
>> I'm sure, I'm getting something wrong.
>> in C its used as xmlFree(whatever);
>> in C++ I use xmlFree((void*)whatever);
>>
>> Zz
>
> extern (C) typedef void function(void *mem) xmlFreeFunc;
> extern (C) xmlFreeFunc xmlFree;

Still no difference.

> Note that I'm not sure the effect of extern (C) on the typedef function pointer, nor on the extern'd variable there.  Someone clarify this?
>
> I gather you're trying to hook up to an external XML parsing/validating library, something like MSXML.  Is it a LIB or DLL file?  Evidence from your post suggests to me that you should have a LIB, otherwise you wouldn't be extern-ing a variable called 'xmlFree'.  This is why you're getting an Access Violation - you're trying to jump to an address that isn't holding valid code and hasn't been initialized yet.
>
> If you have a LIB file, then make sure it's in the OMF format (not the MS "standard" COFF format) and specify its filename on the DMD compiler's commandline when compiling your application.  I hear you can get a COFF-to-OMF conversion utility from the Digital Mars utility CD.

The library is in OMF format, I can parse a document using Llibxml2 and all other functions except for xmlFree work.

Regards,
ZZ


December 28, 2005
"John C" <johnch_atms@hotmail.com> wrote in message news:dor53h$2u2f$1@digitaldaemon.com...
> "Zz" <junkie@noware.com> wrote in message news:dopg4j$3jp$1@digitaldaemon.com...
>> Hi,
>>
>> I have the following in a c header file. (the macros have been expanded).
>>
>> typedef void(__cdecl *xmlFreeFunc)(void *mem);
>> __declspec(dllimport) extern xmlFreeFunc xmlFree;
>
> The D translation is fairly straightforward:
>
>    pragma(lib, "libxml2.lib");
>
>    extern(C) alias void function(void* mem) xmlFreeFunc;
>    extern(C) void xmlFree(void* mem);

This is exactly what I had.

> Although http://www.digitalmars.com/d/windows.html says that __declspec(dllimport) should become 'export'. So if the above fails (although it worked for me with the Windows binaries from http://www.zlatkovic.com/libxml.en.html), try this:
>
>    export void xmlFree(void* mem);

Are you actually using xmlFree?

All other functions in xmlTextReder interface work for me, are you by any chance calling using xmlTextReaderName or xmlTextReaderValue these two functions leave it up to the caller to release the memory with xmlFree.

You can parse and validate a document without calling xmlFree but a lot of memory will be used if you are parsing large files (my files are very large 300+Mb).

I also tried using the dll (copied the way mango wraps ICU) but ran into the same problem.

Maybe I should use xmlTextReaderConstName and xmlTextReaderConstValue.

Thanks
Zz


December 28, 2005
"Zz" <junkie@noware.com> wrote in message news:dotj8u$23k8$1@digitaldaemon.com...
>
> "John C" <johnch_atms@hotmail.com> wrote in message news:dor53h$2u2f$1@digitaldaemon.com...
>> "Zz" <junkie@noware.com> wrote in message news:dopg4j$3jp$1@digitaldaemon.com...
>>> Hi,
>>>
>>> I have the following in a c header file. (the macros have been expanded).
>>>
>>> typedef void(__cdecl *xmlFreeFunc)(void *mem);
>>> __declspec(dllimport) extern xmlFreeFunc xmlFree;
>>
>> The D translation is fairly straightforward:
>>
>>    pragma(lib, "libxml2.lib");
>>
>>    extern(C) alias void function(void* mem) xmlFreeFunc;
>>    extern(C) void xmlFree(void* mem);
>
> This is exactly what I had.
>
>> Although http://www.digitalmars.com/d/windows.html says that __declspec(dllimport) should become 'export'. So if the above fails (although it worked for me with the Windows binaries from http://www.zlatkovic.com/libxml.en.html), try this:
>>
>>    export void xmlFree(void* mem);
>
> Are you actually using xmlFree?

I thought I was, but I just searched my project and discovered I don't call xmlFree. A quick test just now shows that, as you say, calling xmlFree causes an AV.

>
> All other functions in xmlTextReder interface work for me, are you by any chance calling using xmlTextReaderName or xmlTextReaderValue these two functions leave it up to the caller to release the memory with xmlFree.

I used the const versions of those functions.

>
> You can parse and validate a document without calling xmlFree but a lot of memory will be used if you are parsing large files (my files are very large 300+Mb).
>
> I also tried using the dll (copied the way mango wraps ICU) but ran into the same problem.

As did I (using GetProcAddress).

>
> Maybe I should use xmlTextReaderConstName and xmlTextReaderConstValue.
>
> Thanks
> Zz

Does using std.c.stdlib.free instead get rid of the access violation? I've seen a Pascal translation use that.

Sorry I can't be more help. I've drawn a blank as well.


December 29, 2005
"John C" <johnch_atms@hotmail.com> wrote in message news:dottet$2ban$1@digitaldaemon.com...
> "Zz" <junkie@noware.com> wrote in message news:dotj8u$23k8$1@digitaldaemon.com...
>>
>> "John C" <johnch_atms@hotmail.com> wrote in message news:dor53h$2u2f$1@digitaldaemon.com...
>>> "Zz" <junkie@noware.com> wrote in message news:dopg4j$3jp$1@digitaldaemon.com...
>>>> Hi,
>>>>
>>>> I have the following in a c header file. (the macros have been expanded).
>>>>
>>>> typedef void(__cdecl *xmlFreeFunc)(void *mem);
>>>> __declspec(dllimport) extern xmlFreeFunc xmlFree;
>>>
>>> The D translation is fairly straightforward:
>>>
>>>    pragma(lib, "libxml2.lib");
>>>
>>>    extern(C) alias void function(void* mem) xmlFreeFunc;
>>>    extern(C) void xmlFree(void* mem);
>>
>> This is exactly what I had.
>>
>>> Although http://www.digitalmars.com/d/windows.html says that __declspec(dllimport) should become 'export'. So if the above fails (although it worked for me with the Windows binaries from http://www.zlatkovic.com/libxml.en.html), try this:
>>>
>>>    export void xmlFree(void* mem);
>>
>> Are you actually using xmlFree?
>
> I thought I was, but I just searched my project and discovered I don't call xmlFree. A quick test just now shows that, as you say, calling xmlFree causes an AV.
>
>>
>> All other functions in xmlTextReder interface work for me, are you by any chance calling using xmlTextReaderName or xmlTextReaderValue these two functions leave it up to the caller to release the memory with xmlFree.
>
> I used the const versions of those functions.
>
>>
>> You can parse and validate a document without calling xmlFree but a lot of memory will be used if you are parsing large files (my files are very large 300+Mb).
>>
>> I also tried using the dll (copied the way mango wraps ICU) but ran into the same problem.
>
> As did I (using GetProcAddress).
>
>>
>> Maybe I should use xmlTextReaderConstName and xmlTextReaderConstValue.
>>
>> Thanks
>> Zz
>
> Does using std.c.stdlib.free instead get rid of the access violation? I've seen a Pascal translation use that.

> Sorry I can't be more help. I've drawn a blank as well.

It works okay with xmlTextReaderConstName and xmlTextReaderConstValue since the user does not have to explicitly call xmlFree.

Zz