• C

C dll call freezes

I have the following two codes (C):
for a DLL:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

__declspec(dllexport) char* Echo(char* in) {
      char* out;
      out = "Sent: ";
      strcat(out, in);
      return(out);
}

And for an exe that calls the DLL:

#include <stdio.h>
#include <string.h>
#include <windows.h>

typedef char* (*EchoFunc)(char*);

int main(int argc, char *argv[]) {
      EchoFunc _EchoFunc;
      HINSTANCE hInstLibrary = LoadLibrary("Example.dll");
      if (hInstLibrary) {
            _EchoFunc = (EchoFunc)GetProcAddress(hInstLibrary, "Echo");
            if (_EchoFunc) {
                  char* in = "in test.";
                  fprintf(stdout, _EchoFunc(in));
            }
            FreeLibrary(hInstLibrary);
      } else {
            fprintf(stdout, "Example.dll Failed To Load!");
      }
      return 0;
}

Im using MingW TDM-GCC 5.9.2 32 bit for compilation, both files are produced without warnings or errors, but when I call the executable it freezes, I tried to use malloc too for the strings size in the DLL but didn't helped, can anyone help me with this?
Thank you in advance.
KEFEAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Julian HansenCommented:
Do you have debugging capabilities? If you step through with a debugger can you see where it freezes? Failing that - have you tried putting in output statements to see how far it gets?
0
Karrtik IyerSoftware ArchitectCommented:
Try this as your echo function, the array initializer list may not work if you are not using C++ 11. so remove that in case you get that as compilation error.
char* DLL_EXPORT Echo(char* in) {
    const char* const SentString  = "Sent: ";
    char* out = NULL;
    if(in!=NULL)
      out = new char[strlen(SentString) + strlen(in) + 1]{0};//If compilation error use without {0}
    else
      out = new char[strlen(SentString) + 1]{0};//if compilation error use without {0}
      //out = "Sent: ";
      strcpy(out, SentString);
    if(in)
      strcat(out, in);
      return(out);
}
0
KEFEAuthor Commented:
Hello Karrtik, unfortunatlly this isn't C++ code, it is standard ANSI C.

Hello, Julian, I don't have debuging capabilities, but the executable exits with return 3221225477 (0xC0000005) which is STATUS_ACCESS_VIOLATION.
0
Karrtik IyerSoftware ArchitectCommented:
Hi Kefe,
Try this, I have removed new and use malloc, the problem is you are using the out variable without allocating any memory for it hence it has violation issues. Also to assign a string to a character pointer after declaration of variable you need to use string copy, instead of direct assignment. ( out = "Sent: ";) This is equivalent to trying to assignbase address of the char* to a const string, if you had done char* out = "Sent: " it would work, but then the strcat would fail since out would not have enough memory to hold the extra characters. Hope this is helpful.
__declspec(dllexport) char* Echo(char* in) {
    const char* const SentString  = "Sent: ";
    char* out = NULL;
    if(in!=NULL)
      out = (char*)malloc(strlen(SentString) + strlen(in) + 1);//new char[strlen(SentString) + strlen(in) + 1]{0};
    else
      out = (char*)malloc(strlen(SentString) + 1);//new char[strlen(SentString) + 1]{0};
      //out = "Sent: ";
      strcpy(out, SentString);
    if(in)
      strcat(out, in);
      return(out);
}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
KEFEAuthor Commented:
Thank you very much, now it works, as I said I tried to use malloc but it seems I din't used it correctly, also I wasn't declaring the char variable correctly.
Thank you again for your help.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.