Guest User

Untitled

a guest
Dec 23rd, 2012
70
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //main.cpp
  2. extern "C" {
  3. __declspec(dllimport) int __stdcall WriteConsoleW(void* hConsoleOutput, const void *lpBuffer, unsigned int nNumberOfCharsToWrite, unsigned int* lpNumberOfCharsWritten, void* lpReserved);
  4. }
  5.  
  6. int main() {
  7. WriteConsoleW(nullptr, nullptr, 0, nullptr);
  8. }
  9.  
  10. //API.cpp
  11. clang::CompilerInstance ci;
  12.  
  13. clang::FileSystemOptions fso;
  14.  
  15. clang::FileManager fm(fso);
  16.  
  17. std::string errors;
  18. llvm::raw_string_ostream error_stream(errors);
  19. clang::DiagnosticOptions diagopts;
  20. clang::TextDiagnosticPrinter printer(error_stream, &diagopts);
  21. llvm::IntrusiveRefCntPtr<clang::DiagnosticIDs> diagids(new clang::DiagnosticIDs);
  22. clang::DiagnosticsEngine engine(diagids, &diagopts, &printer, false);
  23.  
  24. clang::SourceManager sm(engine, fm);
  25.  
  26. clang::LangOptions langopts;
  27. langopts.CPlusPlus = true;
  28. langopts.CPlusPlus0x = true;
  29.  
  30. clang::TargetOptions target;
  31. target.Triple = llvm::sys::getDefaultTargetTriple();
  32. auto targetinfo = clang::TargetInfo::CreateTargetInfo(engine, &target);
  33.  
  34. auto headeropts = llvm::IntrusiveRefCntPtr<clang::HeaderSearchOptions>(new clang::HeaderSearchOptions());
  35. clang::HeaderSearch hs(headeropts, fm, engine, langopts, targetinfo);
  36.  
  37. auto x = llvm::IntrusiveRefCntPtr<clang::PreprocessorOptions>(new clang::PreprocessorOptions());
  38. clang::Preprocessor p(x, engine, langopts, targetinfo, sm, hs, ci);
  39.  
  40. clang::ASTContext astcon(langopts, sm, targetinfo, p.getIdentifierTable(), p.getSelectorTable(), p.getBuiltinInfo(), 1000);
  41. clang::ASTConsumer semacons;
  42. semacons.Initialize(astcon);
  43. clang::Sema sema(p, astcon, semacons, clang::TranslationUnitKind::TU_Complete);
  44.  
  45. sm.createMainFileID(fm.getFile("main.cpp"));
  46. engine.getClient()->BeginSourceFile(langopts, &p);
  47. clang::ParseAST(sema);
  48. engine.getClient()->EndSourceFile();
  49.  
  50. auto identinfo = p.getIdentifierInfo("WriteConsoleW");
  51. auto fileentry = fm.getFile("main.cpp");
  52. auto fileid = sm.translateFile(fileentry);
  53. auto sloc = sm.getLocForEndOfFile(fileid);
  54. clang::LookupResult lr(sema, clang::DeclarationNameInfo(identinfo, sloc), clang::Sema::LookupNameKind::LookupOrdinaryName);
  55.  
  56. auto result = sema.LookupName(lr, sema.TUScope);
  57. if (!result) __debugbreak(); // Fail
RAW Paste Data