Please login or register.

Login with username, password and session length
Advanced search  

News:

This forum provides RSS feed. To query recent posts use this url. More...


Pages: [1] 2  All

Author Topic: Problems with DLL programming...  (Read 13560 times)

0 Members and 1 Guest are viewing this topic.

McCoy

  • The cocido eater
  • Frequent poster
  • ****
  • Karma: 0
  • Offline Offline
  • Gender: Male
  • Posts: 365
  • Spurrrrrrring
    • View Profile
    • Spur Games
Problems with DLL programming...
« on: July 14, 2003, 12:01:11 AM »

I'm triying to do that nifty file-writer DLL. BTW I have this:

Code: [Select]
#include <fstream>
#include <string>

using namespace std;

extern "C" __declspec(dllexport) int write_file(string fileName, string stringToWrite)
{
   ofstream file(fileName.c_str());
   file << stringToWrite << endl;
   file.close();
   return 1;

}


It's still pretty rough, no exception handling and anything, but I was just starting & testing. It compiles OK and all that, but when I do in WME:

Code: [Select]
external "filehandle.dll" cdecl int write_file(string fileName, string stringToWrite);

write_file("test.txt","testing testing");

I get LOTS of errors, and I mean LOTS!! And it creates a file called "testing testing" or whatever you introduced has stringToWrite, with garbage inside (well, in fact, it ever has this inside:
Logged

Click here to sign my sig!

Mnemonic

  • WME developer
  • Administrator
  • Addicted to WME forum
  • *
  • Karma: 41
  • Offline Offline
  • Gender: Male
  • Posts: 5683
    • View Profile
    • Dead:Code Site
Re:Problems with DLL programming...
« Reply #1 on: July 14, 2003, 08:15:29 AM »

Yes, you should use char* for parameters. Frankly, I don't know how the STL strings are handled internally. Please use char* and you can then easily convert them to string inside your function, if you need to.
Logged
Yes, I do have a twitter account
Please don't send me technical questions in private messages, use the forum. ::wave

McCoy

  • The cocido eater
  • Frequent poster
  • ****
  • Karma: 0
  • Offline Offline
  • Gender: Male
  • Posts: 365
  • Spurrrrrrring
    • View Profile
    • Spur Games
Re:Problems with DLL programming...
« Reply #2 on: July 14, 2003, 11:55:56 AM »

Ok, now I have this

Code: [Select]
extern "C" __declspec(dllexport) int write_file(vector<char> fileName, vector<char> stringToWrite)

{

   string fileName2, stringToWrite2;

   for(int i=0; i<fileName.size(); i++){

      fileName2[i]=fileName[i];
   }

   for(int j=0; j<stringToWrite.size(); j++){

      stringToWrite2[j]=stringToWrite[j];
   }


   ofstream file(fileName2.c_str());
   file << stringToWrite2 << endl;
   file.close();
   return 1;

}

And I get this:

---------------------------------------------------------
---------- WME crash report: 14-07-2003, 12:48 ----------
---------------------------------------------------------
wme.exe caused a EXCEPTION_ACCESS_VIOLATION in module filehandle.dll at 001B:01DF1807, write_file()+119 byte(s), D:\Archivos de programa\Microsoft Visual Studio\MyProjects\filehandle\filehandle.cpp, line 24+28 byte(s)
EAX=01E62030  EBX=00DE1808  ECX=00000000  EDX=0012F974  ESI=00DE0BD0
EDI=0012F990  EBP=0012F990  ESP=0012F884  EIP=01DF1807  FLG=00010246
CS=001B   DS=0023  SS=0023  ES=0023   FS=0038  GS=0000
Stack trace:
001B:01DF1807 (0x00BA9FF0 0x00DE0BD0 0x00DE0598 0x00DE0D00) filehandle.dll, write_file()+119 byte(s), D:\Archivos de programa\Microsoft Visual Studio\MyProjects\filehandle\filehandle.cpp, line 24+28 byte(s)
001B:00443351 (0x00DE0D38 0x00000008 0x01DF10DC 0x0012F9DA) wme.exe
001B:004431D8 (0x00DE0598 0x00DE0570 0x00DE1808 0x00000000) wme.exe
001B:00441D68 (0x0047E7F6 0x76B01B40 0x77D16349 0x00040596) wme.exe
001B:0043499B (0x00000000 0x00000000 0x00DE1938 0x00BA2540) wme.exe
001B:00BA6A20 (0x00434B40 0x00434DE0 0x00433E30 0x00435C70) <UNKNOWN>
001B:00433EB0 (0x082444F6 0x56097401 0xFFF1ABE8 0x04C483FF) wme.exe
001B:FFFFF3B8 (0x00000000 0x00000000 0x00000000 0x00000000) <UNKNOWN>


what's wrong now?
Logged

Click here to sign my sig!

Mnemonic

  • WME developer
  • Administrator
  • Addicted to WME forum
  • *
  • Karma: 41
  • Offline Offline
  • Gender: Male
  • Posts: 5683
    • View Profile
    • Dead:Code Site
Re:Problems with DLL programming...
« Reply #3 on: July 14, 2003, 03:18:14 PM »

I said char*, not vector of chars ;) This should work:

Code: [Select]
extern "C" __declspec(dllexport) int write_file(char* fileName, char* stringToWrite)
{   
   ofstream file(fileName);
   file << stringToWrite << endl;
   file.close();
   return 1;
}


If you need to convert char* to STL string, do just:

Code: [Select]
string stringToWrite2(stringToWrite);
Logged
Yes, I do have a twitter account
Please don't send me technical questions in private messages, use the forum. ::wave

McCoy

  • The cocido eater
  • Frequent poster
  • ****
  • Karma: 0
  • Offline Offline
  • Gender: Male
  • Posts: 365
  • Spurrrrrrring
    • View Profile
    • Spur Games
Re:Problems with DLL programming...
« Reply #4 on: July 14, 2003, 04:20:43 PM »

but char* is not a vector of chars, but fixed?  ??? Anyway, that works... now I have to make something to put multiple lines and not only one... and \n does not seem to work...  :P
Logged

Click here to sign my sig!

Mnemonic

  • WME developer
  • Administrator
  • Addicted to WME forum
  • *
  • Karma: 41
  • Offline Offline
  • Gender: Male
  • Posts: 5683
    • View Profile
    • Dead:Code Site
Re:Problems with DLL programming...
« Reply #5 on: July 14, 2003, 05:20:37 PM »

but char* is not a vector of chars, but fixed?  ???
It's a pointer to the array of chars, terminated by zero. That's how WME passes strings to the DLL's.


Anyway, that works... now I have to make something to put multiple lines and not only one... and \n does not seem to work...  :P
Use ~n instead in the WME script.
Logged
Yes, I do have a twitter account
Please don't send me technical questions in private messages, use the forum. ::wave

McCoy

  • The cocido eater
  • Frequent poster
  • ****
  • Karma: 0
  • Offline Offline
  • Gender: Male
  • Posts: 365
  • Spurrrrrrring
    • View Profile
    • Spur Games
Re:Problems with DLL programming...
« Reply #6 on: July 14, 2003, 06:07:49 PM »

Use ~n instead in the WME script.

Great! Now heading for the read_file function...  ;D
Logged

Click here to sign my sig!

McCoy

  • The cocido eater
  • Frequent poster
  • ****
  • Karma: 0
  • Offline Offline
  • Gender: Male
  • Posts: 365
  • Spurrrrrrring
    • View Profile
    • Spur Games
Re:Problems with DLL programming...
« Reply #7 on: July 22, 2003, 01:24:38 PM »

Ok, So far I have this for the write_fule function:

Code: [Select]
#include "dll.h"
#include <windows.h>
#include <string>
#include <vector>
#include <fstream>
#include <iostream>

using namespace std;

extern "C" __declspec(dllexport) const char* read_file(char* fileName)
{
  char * buffer;
  long size;

  ifstream file (fileName,ifstream::binary);

  file.seekg(0,ifstream::end);
  size=file.tellg();
  file.seekg(0);

  buffer = new char [size];

  file.read (buffer,size);

  file.close();
  return buffer;
 
  delete[] buffer;


}


Which seems to work, it returns spaces, ~n, and all that, and the only limit for the file length is your computer's memory, but... it return a weird character at the end of every line!!! I cant test it right now, but I tested it in Spain and it worked that way. Please, Jan, what can I do? I don't know why this happens... maybe because I'm manipulating the string in binary mode? I dunno...  :P

I'm using Dev C++, just in case...
Logged

Click here to sign my sig!

Mnemonic

  • WME developer
  • Administrator
  • Addicted to WME forum
  • *
  • Karma: 41
  • Offline Offline
  • Gender: Male
  • Posts: 5683
    • View Profile
    • Dead:Code Site
Re:Problems with DLL programming...
« Reply #8 on: July 22, 2003, 02:20:59 PM »

Hm, you really should open the file in text mode, not binary, so that the nwline characters get converted correctly.
Also, the delete[] buffer; line will never get executed, because it's after the return statement (it's a dead-code :-)
Logged
Yes, I do have a twitter account
Please don't send me technical questions in private messages, use the forum. ::wave

McCoy

  • The cocido eater
  • Frequent poster
  • ****
  • Karma: 0
  • Offline Offline
  • Gender: Male
  • Posts: 365
  • Spurrrrrrring
    • View Profile
    • Spur Games
Re:Problems with DLL programming...
« Reply #9 on: July 22, 2003, 03:55:22 PM »

Also, the delete[] buffer; line will never get executed, because it's after the return statement (it's a dead-code :-)

The memory space will be released automatically?
Logged

Click here to sign my sig!

Mnemonic

  • WME developer
  • Administrator
  • Addicted to WME forum
  • *
  • Karma: 41
  • Offline Offline
  • Gender: Male
  • Posts: 5683
    • View Profile
    • Dead:Code Site
Re:Problems with DLL programming...
« Reply #10 on: July 23, 2003, 08:16:59 AM »

The memory space will be released automatically?

Nope, it won't. Maybe it would be better to do it other way; to read the file line by line. You'd have to create four functions, like OpenFile, CloseFile, ReadLine, IsEndOfFile and call them from the script in a "while" cycle, if you know what I mean.
Logged
Yes, I do have a twitter account
Please don't send me technical questions in private messages, use the forum. ::wave

McCoy

  • The cocido eater
  • Frequent poster
  • ****
  • Karma: 0
  • Offline Offline
  • Gender: Male
  • Posts: 365
  • Spurrrrrrring
    • View Profile
    • Spur Games
Re:Problems with DLL programming...
« Reply #11 on: July 23, 2003, 09:15:46 AM »

Yeah, doing the while! file.eof cycle in WME instead of in the function nope? owww... well, I'll see when I come back to Spain. Thanx!
Logged

Click here to sign my sig!

Mnemonic

  • WME developer
  • Administrator
  • Addicted to WME forum
  • *
  • Karma: 41
  • Offline Offline
  • Gender: Male
  • Posts: 5683
    • View Profile
    • Dead:Code Site
Re:Problems with DLL programming...
« Reply #12 on: July 23, 2003, 11:01:10 AM »

Yes, that's what I meant. You'd have a static global variable for one line which will be passed to WME. And that way you'll overcome the dynamic memory allocation troubles.
« Last Edit: July 23, 2003, 11:02:16 AM by Mnemonic »
Logged
Yes, I do have a twitter account
Please don't send me technical questions in private messages, use the forum. ::wave

McCoy

  • The cocido eater
  • Frequent poster
  • ****
  • Karma: 0
  • Offline Offline
  • Gender: Male
  • Posts: 365
  • Spurrrrrrring
    • View Profile
    • Spur Games
Re:Problems with DLL programming...
« Reply #13 on: July 24, 2003, 09:06:45 AM »

Mhh.... Yeah, got it. I'll try when I come back.
Logged

Click here to sign my sig!

creatorbri

  • Occasional poster
  • **
  • Karma: 0
  • Offline Offline
  • Posts: 61
  • I'm a llama!
    • View Profile
Re:Problems with DLL programming...
« Reply #14 on: August 18, 2003, 07:33:41 PM »

This is probably a very stupid question, but -- can I ask what you guys are talking about exactly? Obviously McCoy is trying to write a DLL file containing an external function that reads a file, but I'm not sure I understand what its purpose is. Is he writing something for a future version of WME? Or can DLLs be used within the WME scripting language somehow? Or is this totally not related to WME at all?
Logged
Pages: [1] 2  All
 

Page created in 0.04 seconds with 18 queries.